Skip to content

Commit c0dab06

Browse files
committed
add: 19.remove-nth-node-from-end-of-list(cpp)
1 parent 9979157 commit c0dab06

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
#### 链表拓展题目
109109

110110
- [x] [2.两数相加](./problems/2.add-two-numbers.md)
111+
- [x] [19.删除链表的倒数第 N 个结点](./problems/0019.remove-nth-node-from-end-of-list.md)
111112
- [x] [21.合并两个有序链表](./basic/linked-list/ext-merge-two-sorted-lists.md)
112113
- [x] [83.删除排序链表中的重复元素](./basic/linked-list/ext-remove-duplicates-from-sorted-list.md)
113114
- [x] [206.反转链表](./basic/linked-list/ext-reverse-linked-list.md)
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# 19. 删除链表的倒数第 N 个结点
2+
3+
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
4+
5+
## 题目描述
6+
7+
```
8+
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
9+
10+
进阶:你能尝试使用一趟扫描实现吗?
11+
12+
 
13+
14+
示例 1:
15+
16+
17+
输入:head = [1,2,3,4,5], n = 2
18+
输出:[1,2,3,5]
19+
示例 2:
20+
21+
输入:head = [1], n = 1
22+
输出:[]
23+
示例 3:
24+
25+
输入:head = [1,2], n = 1
26+
输出:[1]
27+
 
28+
29+
提示:
30+
31+
链表中结点的数目为 sz
32+
1 <= sz <= 30
33+
0 <= Node.val <= 100
34+
1 <= n <= sz
35+
36+
来源:力扣(LeetCode)
37+
链接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list
38+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
39+
```
40+
41+
## 方法1: 快慢指针
42+
43+
### 思路
44+
45+
**找到倒数第 N 个节点**
46+
47+
- 要删除倒数第 N 个节点,第一步要先找到倒数第 N 个节点(实际上对于单向链表,还需要找到倒数第 N+1 个节点才能完成删除倒数第 N 个节点的操作)。
48+
- 简单的思路是,遍历确定链表长度 L,通过计算得到倒数第 N 个节点是顺数第 L-N 个节点。
49+
- 快慢指针的思路是,定义两个指针,快指针先走 N 步,剩下的路程自然是 L-N,这时再让快慢指针同时走,等快指针走路末尾时,慢指针走过的路程刚好是 L-N,也就是刚好走到倒数第 N 个节点。
50+
51+
**使用 dummy 节点来避免单独处理头节点**
52+
53+
假如要删除的节点刚好是头节点的话,要额外处理就很麻烦。但如果事先在头节点前面添加一个 dummy 节点,就可以把头节点当成一个普通节点来处理了,这是链表题中常用的技巧。
54+
55+
### 复杂度分析
56+
57+
- 时间复杂度:$O(N)$。
58+
- 空间复杂度:$O(1)$。
59+
60+
### 代码
61+
62+
C++ Code
63+
64+
```cpp
65+
/**
66+
* Definition for singly-linked list.
67+
* struct ListNode {
68+
* int val;
69+
* ListNode *next;
70+
* ListNode() : val(0), next(nullptr) {}
71+
* ListNode(int x) : val(x), next(nullptr) {}
72+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
73+
* };
74+
*/
75+
class Solution {
76+
public:
77+
ListNode* removeNthFromEnd(ListNode* head, int n) {
78+
ListNode *fast = head, *slow = head;
79+
while (n--) {
80+
fast = fast->next;
81+
}
82+
// 额外处理头部节点
83+
if (!fast) {
84+
ListNode *new_head = head->next;
85+
head->next = nullptr;
86+
return new_head;
87+
}
88+
while (fast->next) {
89+
fast = fast->next;
90+
slow = slow->next;
91+
}
92+
ListNode *target = slow->next;
93+
slow->next = target->next;
94+
target->next = nullptr;
95+
return head;
96+
}
97+
};
98+
```
99+
100+
使用 dummy 节点
101+
102+
```cpp
103+
/**
104+
* Definition for singly-linked list.
105+
* struct ListNode {
106+
* int val;
107+
* ListNode *next;
108+
* ListNode() : val(0), next(nullptr) {}
109+
* ListNode(int x) : val(x), next(nullptr) {}
110+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
111+
* };
112+
*/
113+
class Solution {
114+
public:
115+
ListNode* removeNthFromEnd(ListNode* head, int n) {
116+
ListNode *dummy = new ListNode(0, head);
117+
ListNode *fast = dummy, *slow = dummy;
118+
119+
// 快指针先走 n 步
120+
while (n-- > 0) {
121+
fast = fast->next;
122+
}
123+
// 当快指针走到尾节点时
124+
// 慢指针刚好走到倒数 n+1 个节点
125+
while (fast && fast->next) {
126+
fast = fast->next;
127+
slow = slow->next;
128+
}
129+
ListNode *target = slow->next;
130+
slow->next = target->next;
131+
target->next = nullptr;
132+
return dummy->next;
133+
}
134+
};
135+
```
136+
137+
更多题解可以访问:[https://github.com/suukii/91-days-algorithm](https://github.com/suukii/91-days-algorithm)

0 commit comments

Comments
 (0)