Skip to content

Commit f0e66e1

Browse files
committed
feat: add solutions to lc problem: No.1474. Delete N Nodes After M Nodes of a Linked List
1 parent ede8a8e commit f0e66e1

File tree

4 files changed

+155
-35
lines changed

4 files changed

+155
-35
lines changed

Diff for: solution/1400-1499/1474.Delete N Nodes After M Nodes of a Linked List/README.md

+53-3
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,77 @@
6666
<li><code>1 <= m,n <= 1000</code></li>
6767
</ul>
6868

69-
7069
## 解法
7170

7271
<!-- 这里可写通用的实现逻辑 -->
7372

73+
遍历链表,修改指针指向即可。
74+
7475
<!-- tabs:start -->
7576

7677
### **Python3**
7778

7879
<!-- 这里可写当前语言的特殊实现逻辑 -->
7980

8081
```python
81-
82+
# Definition for singly-linked list.
83+
# class ListNode:
84+
# def __init__(self, val=0, next=None):
85+
# self.val = val
86+
# self.next = next
87+
class Solution:
88+
def deleteNodes(self, head: ListNode, m: int, n: int) -> ListNode:
89+
pre = head
90+
while pre:
91+
for i in range(m - 1):
92+
if pre:
93+
pre = pre.next
94+
if pre is None:
95+
return head
96+
cur = pre
97+
for i in range(n):
98+
if cur:
99+
cur = cur.next
100+
pre.next = None if cur is None else cur.next
101+
pre = pre.next
102+
return head
82103
```
83104

84105
### **Java**
85106

86107
<!-- 这里可写当前语言的特殊实现逻辑 -->
87108

88109
```java
89-
110+
/**
111+
* Definition for singly-linked list.
112+
* public class ListNode {
113+
* int val;
114+
* ListNode next;
115+
* ListNode() {}
116+
* ListNode(int val) { this.val = val; }
117+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
118+
* }
119+
*/
120+
class Solution {
121+
public ListNode deleteNodes(ListNode head, int m, int n) {
122+
ListNode pre = head;
123+
while (pre != null) {
124+
for (int i = 0; i < m - 1 && pre != null; ++i) {
125+
pre = pre.next;
126+
}
127+
if (pre == null) {
128+
return head;
129+
}
130+
ListNode cur = pre;
131+
for (int i = 0; i < n && cur != null; ++i) {
132+
cur = cur.next;
133+
}
134+
pre.next = cur == null ? null : cur.next;
135+
pre = pre.next;
136+
}
137+
return head;
138+
}
139+
}
90140
```
91141

92142
### **...**

Diff for: solution/1400-1499/1474.Delete N Nodes After M Nodes of a Linked List/README_EN.md

+51-32
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,23 @@
66

77
<p>Given the&nbsp;<code>head</code>&nbsp;of a linked list and two integers <code>m</code> and <code>n</code>. Traverse the linked list and remove some nodes&nbsp;in the following way:</p>
88

9-
10-
119
<ul>
1210
<li>Start with the head as the current node.</li>
1311
<li>Keep the first <code>m</code> nodes starting with the current node.</li>
1412
<li>Remove the next <code>n</code> nodes</li>
1513
<li>Keep repeating steps 2 and 3 until you reach the end of the list.</li>
1614
</ul>
1715

18-
19-
2016
<p>Return the head of the modified list after removing the mentioned nodes.</p>
2117

22-
23-
2418
<p><strong>Follow up question:</strong> How can you solve this problem by modifying the list in-place?</p>
2519

26-
27-
2820
<p>&nbsp;</p>
2921

3022
<p><strong>Example 1:</strong></p>
3123

32-
33-
3424
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1474.Delete%20N%20Nodes%20After%20M%20Nodes%20of%20a%20Linked%20List/images/sample_1_1848.png" style="width: 620px; height: 95px;" /></strong></p>
3525

36-
37-
3826
<pre>
3927

4028
<strong>Input:</strong> head = [1,2,3,4,5,6,7,8,9,10,11,12,13], m = 2, n = 3
@@ -49,16 +37,10 @@ Continue with the same procedure until reaching the tail of the Linked List.
4937

5038
Head of linked list after removing nodes is returned.</pre>
5139

52-
53-
5440
<p><strong>Example 2:</strong></p>
5541

56-
57-
5842
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1474.Delete%20N%20Nodes%20After%20M%20Nodes%20of%20a%20Linked%20List/images/sample_2_1848.png" style="width: 620px; height: 123px;" /></strong></p>
5943

60-
61-
6244
<pre>
6345

6446
<strong>Input:</strong> head = [1,2,3,4,5,6,7,8,9,10,11], m = 1, n = 3
@@ -67,12 +49,8 @@ Head of linked list after removing nodes is returned.</pre>
6749

6850
<strong>Explanation:</strong> Head of linked list after removing nodes is returned.</pre>
6951

70-
71-
7252
<p><strong>Example 3:</strong></p>
7353

74-
75-
7654
<pre>
7755

7856
<strong>Input:</strong> head = [1,2,3,4,5,6,7,8,9,10,11], m = 3, n = 1
@@ -81,12 +59,8 @@ Head of linked list after removing nodes is returned.</pre>
8159

8260
</pre>
8361

84-
85-
8662
<p><strong>Example 4:</strong></p>
8763

88-
89-
9064
<pre>
9165

9266
<strong>Input:</strong> head = [9,3,7,7,9,10,8,2], m = 1, n = 2
@@ -95,14 +69,10 @@ Head of linked list after removing nodes is returned.</pre>
9569

9670
</pre>
9771

98-
99-
10072
<p>&nbsp;</p>
10173

10274
<p><strong>Constraints:</strong></p>
10375

104-
105-
10676
<ul>
10777
<li>The given linked list will contain between&nbsp;<code>1</code>&nbsp;and&nbsp;<code>10^4</code>&nbsp;nodes.</li>
10878
<li>The value of each node in the linked list will be in the range<code>&nbsp;[1, 10^6]</code>.</li>
@@ -116,13 +86,62 @@ Head of linked list after removing nodes is returned.</pre>
11686
### **Python3**
11787

11888
```python
119-
89+
# Definition for singly-linked list.
90+
# class ListNode:
91+
# def __init__(self, val=0, next=None):
92+
# self.val = val
93+
# self.next = next
94+
class Solution:
95+
def deleteNodes(self, head: ListNode, m: int, n: int) -> ListNode:
96+
pre = head
97+
while pre:
98+
for i in range(m - 1):
99+
if pre:
100+
pre = pre.next
101+
if pre is None:
102+
return head
103+
cur = pre
104+
for i in range(n):
105+
if cur:
106+
cur = cur.next
107+
pre.next = None if cur is None else cur.next
108+
pre = pre.next
109+
return head
120110
```
121111

122112
### **Java**
123113

124114
```java
125-
115+
/**
116+
* Definition for singly-linked list.
117+
* public class ListNode {
118+
* int val;
119+
* ListNode next;
120+
* ListNode() {}
121+
* ListNode(int val) { this.val = val; }
122+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
123+
* }
124+
*/
125+
class Solution {
126+
public ListNode deleteNodes(ListNode head, int m, int n) {
127+
ListNode pre = head;
128+
while (pre != null) {
129+
for (int i = 0; i < m - 1 && pre != null; ++i) {
130+
pre = pre.next;
131+
}
132+
if (pre == null) {
133+
return head;
134+
}
135+
ListNode cur = pre;
136+
for (int i = 0; i < n && cur != null; ++i) {
137+
cur = cur.next;
138+
}
139+
pre.next = cur == null ? null : cur.next;
140+
pre = pre.next;
141+
}
142+
return head;
143+
}
144+
}
126145
```
127146

128147
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode deleteNodes(ListNode head, int m, int n) {
13+
ListNode pre = head;
14+
while (pre != null) {
15+
for (int i = 0; i < m - 1 && pre != null; ++i) {
16+
pre = pre.next;
17+
}
18+
if (pre == null) {
19+
return head;
20+
}
21+
ListNode cur = pre;
22+
for (int i = 0; i < n && cur != null; ++i) {
23+
cur = cur.next;
24+
}
25+
pre.next = cur == null ? null : cur.next;
26+
pre = pre.next;
27+
}
28+
return head;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def deleteNodes(self, head: ListNode, m: int, n: int) -> ListNode:
8+
pre = head
9+
while pre:
10+
for i in range(m - 1):
11+
if pre:
12+
pre = pre.next
13+
if pre is None:
14+
return head
15+
cur = pre
16+
for i in range(n):
17+
if cur:
18+
cur = cur.next
19+
pre.next = None if cur is None else cur.next
20+
pre = pre.next
21+
return head

0 commit comments

Comments
 (0)