-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.js
37 lines (36 loc) · 814 Bytes
/
19.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
/**
* 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) {
while (head) {
let fast = slow = new ListNode(0)
fast = slow = head
while (n) {
n--
fast = fast.next
}
if (fast) {
while (fast && fast.next) {
fast = fast.next
slow = slow.next
}
slow.next = slow.next.next
return head
}
return head.next
}
return null
};
/*
2021/8/13
利用虚拟头节点可以减小消耗同时精简代码
*/