题解 | #删除链表的倒数第n个节点#
删除链表的倒数第n个节点
http://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
/*
* function ListNode(x){
* this.val = x;
* this.next = null;
* }
*/
/**
*
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
function removeNthFromEnd( head , n ) {
// write code here
if(!head)return null
let quick = head;
let slow = head;
for(let i = 0;i<n;i++){
quick = quick.next;
}
//这一步真是太关键了
if(quick == null){
return head.next
}
while(quick.next){
quick = quick.next;
slow = slow.next;
}
slow.next = slow.next.next
return head
}
module.exports = {
removeNthFromEnd : removeNthFromEnd
};