/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
function removeNthFromEnd( head , n ) {
if (head === null) return head;
let fast = head;
// 删除了链表的倒数第 n 个节点
while (fast && n > 0) {
fast = fast.next;
n--;
}
if (n > 0) return null;
let slow = head;
let pre = null; // before nth node
while (fast) {
pre = slow;
fast = fast.next;
slow = slow.next;
}
if (pre === null) return slow.next;
pre.next = slow.next;
return head;
}
module.exports = {
removeNthFromEnd : removeNthFromEnd
};