-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathNo19.remove-nth-node-from-end-of-list.js
61 lines (55 loc) · 1.46 KB
/
No19.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
/*
* Difficulty:
* Medium
*
* Desc:
* Given a linked list, remove the nth 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.
* Try to do this in one pass.
*
* 给出一个链表,删除倒数第 n 位的元素
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/*
* 思路:
* 因为是链表,所以我们不知道其长度,如果直接遍历,过程中也不知道是否到达了倒数第 n 位元素
* 为了减少遍历次数,可以创建两个链表指针,初始化都指向头部
* 然后其中一个指针(B)向后遍历,使其位置超出另一个指针(A)n + 1 位,此时 A、B 之间位置相差 n(A 位于第 1 位,B 位于第 n + 1 位)
* 然后两者同时向后遍历,直到 B 超出范围。此时 A 与链表末尾相差 n 位,即要删除的元素
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
let n1 = head
let n2 = head
let count = 1
let i = n
while (n && n2 && n2.next) {
n2 = n2.next
n -= 1
count += 1
}
while (n2 && n2.next) {
n2 = n2.next
n1 = n1.next
count += 1
}
if (count === i) return n1.next
n1.next = n1.next.next
return head
}