Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid
.
Follow up:
Could you do this in one pass
?
对于删除链表节点的题目, 我们需要知道需删除链表节点的上一个节点。那如何找到需要删除节点的上一个节点呢?
sum - n - 1
表示从正向数过来需删除的链表节点的上一个节点的位数;此外从题目的例子可以得到的线索:
1 -> 2 -> 3 -> 4 -> 5 -> null..第一步: l 与 r 的距离为 n + 1;l rdummy -> 1 -> 2 -> 3 -> 4 -> 5 -> null..第二步: 始终保持 l 与 r 的距离为 n + 1, 向右移动, 直到 r 为 null, 此时 l 的位置就是要删除节点上一个的位置。l rdummy -> 1 -> 2 -> 3 -> 4 -> 5 -> null
/*** Definition for singly-linked list.* function ListNode(val) {* this.val = val;* this.next = null;* }*//*** @param {ListNode} head* @param {number} n* @return {ListNode}*/var removeNthFromEnd = function(head, n) {const dummy = new ListNode(0)dummy.next = headlet l = dummylet r = dummylet offset = n + 1while (offset--) {r = r.nextif (offset > 1 && r === null) {return dummy.next}}while (r) {r = r.nextl = l.next}l.next = l.next.nextreturn dummy.next}
61