File tree 5 files changed +208
-7
lines changed
solution/1400-1499/1474.Delete N Nodes After M Nodes of a Linked List
5 files changed +208
-7
lines changed Original file line number Diff line number Diff line change 72
72
73
73
<!-- 这里可写通用的实现逻辑 -->
74
74
75
- 遍历链表,修改指针指向即可。
75
+ ** 方法一:模拟**
76
+
77
+ 按照题意模拟,遍历链表,每次遍历 $m$ 个节点,然后删除 $n$ 个节点,直到链表尾部。
78
+
79
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。
76
80
77
81
<!-- tabs:start -->
78
82
@@ -90,13 +94,13 @@ class Solution:
90
94
def deleteNodes (self , head : ListNode, m : int , n : int ) -> ListNode:
91
95
pre = head
92
96
while pre:
93
- for i in range (m - 1 ):
97
+ for _ in range (m - 1 ):
94
98
if pre:
95
99
pre = pre.next
96
100
if pre is None :
97
101
return head
98
102
cur = pre
99
- for i in range (n):
103
+ for _ in range (n):
100
104
if cur:
101
105
cur = cur.next
102
106
pre.next = None if cur is None else cur.next
@@ -141,6 +145,75 @@ class Solution {
141
145
}
142
146
```
143
147
148
+ ### ** C++**
149
+
150
+ ``` cpp
151
+ /* *
152
+ * Definition for singly-linked list.
153
+ * struct ListNode {
154
+ * int val;
155
+ * ListNode *next;
156
+ * ListNode() : val(0), next(nullptr) {}
157
+ * ListNode(int x) : val(x), next(nullptr) {}
158
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
159
+ * };
160
+ */
161
+ class Solution {
162
+ public:
163
+ ListNode* deleteNodes(ListNode* head, int m, int n) {
164
+ auto pre = head;
165
+ while (pre) {
166
+ for (int i = 0; i < m - 1 && pre; ++i) {
167
+ pre = pre->next;
168
+ }
169
+ if (!pre) {
170
+ return head;
171
+ }
172
+ auto cur = pre;
173
+ for (int i = 0; i < n && cur; ++i) {
174
+ cur = cur->next;
175
+ }
176
+ pre->next = cur ? cur->next : nullptr;
177
+ pre = pre->next;
178
+ }
179
+ return head;
180
+ }
181
+ };
182
+ ```
183
+
184
+ ### **Go**
185
+
186
+ ```go
187
+ /**
188
+ * Definition for singly-linked list.
189
+ * type ListNode struct {
190
+ * Val int
191
+ * Next *ListNode
192
+ * }
193
+ */
194
+ func deleteNodes(head *ListNode, m int, n int) *ListNode {
195
+ pre := head
196
+ for pre != nil {
197
+ for i := 0; i < m-1 && pre != nil; i++ {
198
+ pre = pre.Next
199
+ }
200
+ if pre == nil {
201
+ return head
202
+ }
203
+ cur := pre
204
+ for i := 0; i < n && cur != nil; i++ {
205
+ cur = cur.Next
206
+ }
207
+ pre.Next = nil
208
+ if cur != nil {
209
+ pre.Next = cur.Next
210
+ }
211
+ pre = pre.Next
212
+ }
213
+ return head
214
+ }
215
+ ```
216
+
144
217
### ** ...**
145
218
146
219
```
Original file line number Diff line number Diff line change @@ -65,13 +65,13 @@ class Solution:
65
65
def deleteNodes (self , head : ListNode, m : int , n : int ) -> ListNode:
66
66
pre = head
67
67
while pre:
68
- for i in range (m - 1 ):
68
+ for _ in range (m - 1 ):
69
69
if pre:
70
70
pre = pre.next
71
71
if pre is None :
72
72
return head
73
73
cur = pre
74
- for i in range (n):
74
+ for _ in range (n):
75
75
if cur:
76
76
cur = cur.next
77
77
pre.next = None if cur is None else cur.next
@@ -114,6 +114,75 @@ class Solution {
114
114
}
115
115
```
116
116
117
+ ### ** C++**
118
+
119
+ ``` cpp
120
+ /* *
121
+ * Definition for singly-linked list.
122
+ * struct ListNode {
123
+ * int val;
124
+ * ListNode *next;
125
+ * ListNode() : val(0), next(nullptr) {}
126
+ * ListNode(int x) : val(x), next(nullptr) {}
127
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
128
+ * };
129
+ */
130
+ class Solution {
131
+ public:
132
+ ListNode* deleteNodes(ListNode* head, int m, int n) {
133
+ auto pre = head;
134
+ while (pre) {
135
+ for (int i = 0; i < m - 1 && pre; ++i) {
136
+ pre = pre->next;
137
+ }
138
+ if (!pre) {
139
+ return head;
140
+ }
141
+ auto cur = pre;
142
+ for (int i = 0; i < n && cur; ++i) {
143
+ cur = cur->next;
144
+ }
145
+ pre->next = cur ? cur->next : nullptr;
146
+ pre = pre->next;
147
+ }
148
+ return head;
149
+ }
150
+ };
151
+ ```
152
+
153
+ ### **Go**
154
+
155
+ ```go
156
+ /**
157
+ * Definition for singly-linked list.
158
+ * type ListNode struct {
159
+ * Val int
160
+ * Next *ListNode
161
+ * }
162
+ */
163
+ func deleteNodes(head *ListNode, m int, n int) *ListNode {
164
+ pre := head
165
+ for pre != nil {
166
+ for i := 0; i < m-1 && pre != nil; i++ {
167
+ pre = pre.Next
168
+ }
169
+ if pre == nil {
170
+ return head
171
+ }
172
+ cur := pre
173
+ for i := 0; i < n && cur != nil; i++ {
174
+ cur = cur.Next
175
+ }
176
+ pre.Next = nil
177
+ if cur != nil {
178
+ pre.Next = cur.Next
179
+ }
180
+ pre = pre.Next
181
+ }
182
+ return head
183
+ }
184
+ ```
185
+
117
186
### ** ...**
118
187
119
188
```
Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode() : val(0), next(nullptr) {}
7
+ * ListNode(int x) : val(x), next(nullptr) {}
8
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
9
+ * };
10
+ */
11
+ class Solution {
12
+ public:
13
+ ListNode* deleteNodes (ListNode* head, int m, int n) {
14
+ auto pre = head;
15
+ while (pre) {
16
+ for (int i = 0 ; i < m - 1 && pre; ++i) {
17
+ pre = pre->next ;
18
+ }
19
+ if (!pre) {
20
+ return head;
21
+ }
22
+ auto cur = pre;
23
+ for (int i = 0 ; i < n && cur; ++i) {
24
+ cur = cur->next ;
25
+ }
26
+ pre->next = cur ? cur->next : nullptr ;
27
+ pre = pre->next ;
28
+ }
29
+ return head;
30
+ }
31
+ };
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * type ListNode struct {
4
+ * Val int
5
+ * Next *ListNode
6
+ * }
7
+ */
8
+ func deleteNodes (head * ListNode , m int , n int ) * ListNode {
9
+ pre := head
10
+ for pre != nil {
11
+ for i := 0 ; i < m - 1 && pre != nil ; i ++ {
12
+ pre = pre .Next
13
+ }
14
+ if pre == nil {
15
+ return head
16
+ }
17
+ cur := pre
18
+ for i := 0 ; i < n && cur != nil ; i ++ {
19
+ cur = cur .Next
20
+ }
21
+ pre .Next = nil
22
+ if cur != nil {
23
+ pre .Next = cur .Next
24
+ }
25
+ pre = pre .Next
26
+ }
27
+ return head
28
+ }
Original file line number Diff line number Diff line change @@ -7,13 +7,13 @@ class Solution:
7
7
def deleteNodes (self , head : ListNode , m : int , n : int ) -> ListNode :
8
8
pre = head
9
9
while pre :
10
- for i in range (m - 1 ):
10
+ for _ in range (m - 1 ):
11
11
if pre :
12
12
pre = pre .next
13
13
if pre is None :
14
14
return head
15
15
cur = pre
16
- for i in range (n ):
16
+ for _ in range (n ):
17
17
if cur :
18
18
cur = cur .next
19
19
pre .next = None if cur is None else cur .next
You can’t perform that action at this time.
0 commit comments