Skip to content

Commit 5b576e3

Browse files
author
luzhipeng
committed
双指针
1 parent 82abc55 commit 5b576e3

3 files changed

+181
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @lc app=leetcode id=19 lang=javascript
3+
*
4+
* [19] Remove Nth Node From End of List
5+
*
6+
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
7+
*
8+
* algorithms
9+
* Medium (34.03%)
10+
* Total Accepted: 360.1K
11+
* Total Submissions: 1.1M
12+
* Testcase Example: '[1,2,3,4,5]\n2'
13+
*
14+
* Given a linked list, remove the n-th node from the end of list and return
15+
* its head.
16+
*
17+
* Example:
18+
*
19+
*
20+
* Given linked list: 1->2->3->4->5, and n = 2.
21+
*
22+
* After removing the second node from the end, the linked list becomes
23+
* 1->2->3->5.
24+
*
25+
*
26+
* Note:
27+
*
28+
* Given n will always be valid.
29+
*
30+
* Follow up:
31+
*
32+
* Could you do this in one pass?
33+
*
34+
*/
35+
/**
36+
* Definition for singly-linked list.
37+
* function ListNode(val) {
38+
* this.val = val;
39+
* this.next = null;
40+
* }
41+
*/
42+
/**
43+
* @param {ListNode} head
44+
* @param {number} n
45+
* @return {ListNode}
46+
*/
47+
var removeNthFromEnd = function(head, n) {
48+
49+
};
50+
359 KB
Loading

removeNthNodeFromEndofList.md

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
## 题目地址
2+
https://leetcode.com/problems/remove-nth-node-from-end-of-list/description
3+
4+
## 题目描述
5+
Given a linked list, remove the n-th node from the end of list and return its head.
6+
7+
Example:
8+
9+
Given linked list: 1->2->3->4->5, and n = 2.
10+
11+
After removing the second node from the end, the linked list becomes 1->2->3->5.
12+
Note:
13+
14+
Given n will always be valid.
15+
16+
Follow up:
17+
18+
Could you do this in one pass?
19+
20+
## 思路
21+
22+
双指针,指针A先移动n次, 指针B再开始移动。当A到达null的时候, 指针b的位置正好是倒数n
23+
24+
我们可以设想假设设定了双指针p和q的话,当q指向末尾的NULL,p与q之间相隔的元素个数为n时,那么删除掉p的下一个指针就完成了要求。
25+
26+
设置虚拟节点dummyHead指向head
27+
28+
设定双指针p和q,初始都指向虚拟节点dummyHead
29+
30+
移动q,直到p与q之间相隔的元素个数为n
31+
32+
同时移动p与q,直到q指向的为NULL
33+
34+
将p的下一个节点指向下下个节点
35+
36+
37+
38+
![19.removeNthNodeFromEndOfList](./assets/19.removeNthNodeFromEndOfList.gif)
39+
40+
(图片来自: https://github.com/MisterBooo/LeetCodeAnimation)
41+
42+
## 关键点解析
43+
44+
1. 链表这种数据结构的特点和使用
45+
46+
2. 使用双指针
47+
48+
3. 使用一个dummyHead简化操作
49+
50+
## 代码
51+
52+
53+
```js
54+
/*
55+
* @lc app=leetcode id=19 lang=javascript
56+
*
57+
* [19] Remove Nth Node From End of List
58+
*
59+
* https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
60+
*
61+
* algorithms
62+
* Medium (34.03%)
63+
* Total Accepted: 360.1K
64+
* Total Submissions: 1.1M
65+
* Testcase Example: '[1,2,3,4,5]\n2'
66+
*
67+
* Given a linked list, remove the n-th node from the end of list and return
68+
* its head.
69+
*
70+
* Example:
71+
*
72+
*
73+
* Given linked list: 1->2->3->4->5, and n = 2.
74+
*
75+
* After removing the second node from the end, the linked list becomes
76+
* 1->2->3->5.
77+
*
78+
*
79+
* Note:
80+
*
81+
* Given n will always be valid.
82+
*
83+
* Follow up:
84+
*
85+
* Could you do this in one pass?
86+
*
87+
*/
88+
/**
89+
* Definition for singly-linked list.
90+
* function ListNode(val) {
91+
* this.val = val;
92+
* this.next = null;
93+
* }
94+
*/
95+
/**
96+
* @param {ListNode} head
97+
* @param {number} n
98+
* @return {ListNode}
99+
*/
100+
var removeNthFromEnd = function(head, n) {
101+
let i = -1;
102+
const noop = {
103+
next: null
104+
};
105+
106+
const dummyHead = new ListNode(); // 增加一个dummyHead 简化操作
107+
dummyHead.next = head;
108+
109+
let currentP1 = dummyHead;
110+
let currentP2 = dummyHead;
111+
112+
113+
while (currentP1) {
114+
115+
if (i === n) {
116+
currentP2 = currentP2.next;
117+
}
118+
119+
if (i !== n) {
120+
i++;
121+
}
122+
123+
currentP1 = currentP1.next;
124+
}
125+
126+
currentP2.next = ((currentP2 || noop).next || noop).next;
127+
128+
return dummyHead.next;
129+
};
130+
131+
```

0 commit comments

Comments
 (0)