-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.删除链表的倒数第-n-个结点.js
55 lines (46 loc) · 1.16 KB
/
19.删除链表的倒数第-n-个结点.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
/*
* @lc app=leetcode.cn id=19 lang=javascript
*
* [19] 删除链表的倒数第 N 个结点
*/
// @lc code=start
/**
* 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}
*/
function ListNode(val, next) {
this.val = val === undefined ? 0 : val
this.next = next === undefined ? null : next
}
var removeNthFromEnd = function(head, n) {
// 💥: must use dummy head to avoid null pointer
// try to remove dummy and you will see the error
const dummy = new ListNode(-1)
dummy.next = head
// need to use n+1 since we take dummy head into account
const nthNodeFromEnd = findNthNodeFromEnd(dummy, n + 1)
nthNodeFromEnd.next = nthNodeFromEnd.next.next
return dummy.next
function findNthNodeFromEnd(head, n) {
let slow = head
let fast = head
for (let i = 0; i < n; i++) {
fast = fast.next
}
while (fast) {
fast = fast.next
slow = slow.next
}
return slow
}
}
// @lc code=end
module.exports = { removeNthFromEnd }