题目描述
代码参考
// 可以借助之前,追击的问题,借助返回倒数第k个节点的题的基础上
function removeNthFromEnd( head , n ) {
// write code
let node = new ListNode(-1)
node.next = head
let pre = node
let fast = slow = head
for(let i = 0; i < n; i++) {
if(fast===null) return null
fast = fast.next
}
// 此时让fast和next同时跑
while(fast){
fast = fast.next
slow = slow.next
pre = pre.next
}
// 假设链表的长度足够时,那么此时slow的后面还有节点,slow是我们要删除的节点
pre.next = slow.next
slow.next = null
return node.next
}
标签:pre,node,slow,fast,next,链表,let,BM9,倒数第
From: https://www.cnblogs.com/zx529/p/17011871.html