-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-nth-node-from-end-of-list.js
88 lines (77 loc) · 1.64 KB
/
remove-nth-node-from-end-of-list.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function (head, n) {
// 常规做法 计算链表长度
let dummyNode = new ListNode(-1);
dummyNode.next = head;
let length = getLength(head);
let curr = dummyNode;
for (let i = 1; i < length - n + 1; i++) {
curr = curr.next;
}
curr.next = curr.next.next;
return dummyNode.next;
// 栈
// return stackFn(head);
// 双指针
// return twoPointers(head);
};
// 计算链表长度
function getLength(head) {
let curr = head;
let length = 0;
while (curr) {
length++;
curr = curr.next;
}
return length;
}
// 栈
function stackFn(head) {
let dummyNode = new ListNode(-1);
dummyNode.next = head;
let curr = dummyNode;
let stack = [];
// 入栈
while (curr) {
stack.push(curr);
curr = curr.next;
}
// 出栈
for (let i = 0; i < n; i++) {
stack.pop();
}
// 待删除节点的前置节点
const prev = stack.at(-1);
prev.next = prev.next.next;
return dummyNode.next;
}
// 双指针
var twoPointers = function(head, n) {
let dummyNode = new ListNode(-1);
dummyNode.next = head;
let fast = head;
// 指向哑节点
let slow = dummyNode;
// 前进 n 步
for (let i = 0; i < n; i++) {
fast = fast.next;
}
while (fast) {
fast = fast.next;
slow = slow.next;
}
// 此时,slow 指向待删除节点的前驱节点
slow.next = slow.next.next;
return dummyNode.next;
};