简单思路:快慢指针,第一个先往前走,然后两个指针齐步走,快指针到末尾的时候,满指针指向了要删除的元素,我们还保存了删除元素的前一个元素的指针,这个时候 pre.next = pre.next.next 就可以删除了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 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 result = new ListNode(null);
result.next = head;
let cur = head;
let pre = result;
while (n--) {
head = head.next;
}
while (head) {
head = head.next;
cur = cur.next;
pre = pre.next;
}
pre.next = pre.next.next;
return result.next;
};