Skip to content

Commit 658d9a7

Browse files
committed
feat: add solutions to lc problem: No.0025. Reverse Nodes in k-Group
1 parent 63ea667 commit 658d9a7

File tree

6 files changed

+469
-120
lines changed

6 files changed

+469
-120
lines changed

solution/0000-0099/0025.Reverse Nodes in k-Group/README.md

+174-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
<li><code>1 <= k <= sz</code></li>
6262
</ul>
6363

64-
6564
## 解法
6665

6766
<!-- 这里可写通用的实现逻辑 -->
@@ -73,15 +72,188 @@
7372
<!-- 这里可写当前语言的特殊实现逻辑 -->
7473

7574
```python
76-
75+
# Definition for singly-linked list.
76+
# class ListNode:
77+
# def __init__(self, val=0, next=None):
78+
# self.val = val
79+
# self.next = next
80+
class Solution:
81+
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
82+
def reverseList(head):
83+
pre, p = None, head
84+
while p:
85+
q = p.next
86+
p.next = pre
87+
pre = p
88+
p = q
89+
return pre
90+
91+
dummy = ListNode(next=head)
92+
pre = cur = dummy
93+
while cur.next:
94+
for _ in range(k):
95+
cur = cur.next
96+
if cur is None:
97+
return dummy.next
98+
t = cur.next
99+
cur.next = None
100+
start = pre.next
101+
pre.next = reverseList(start)
102+
start.next = t
103+
pre = start
104+
cur = pre
105+
return dummy.next
77106
```
78107

79108
### **Java**
80109

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

83112
```java
113+
/**
114+
* Definition for singly-linked list.
115+
* public class ListNode {
116+
* int val;
117+
* ListNode next;
118+
* ListNode() {}
119+
* ListNode(int val) { this.val = val; }
120+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
121+
* }
122+
*/
123+
class Solution {
124+
public ListNode reverseKGroup(ListNode head, int k) {
125+
ListNode dummy = new ListNode(0, head);
126+
ListNode pre = dummy, cur = dummy;
127+
while (cur.next != null) {
128+
for (int i = 0; i < k && cur != null; ++i) {
129+
cur = cur.next;
130+
}
131+
if (cur == null) {
132+
return dummy.next;
133+
}
134+
ListNode t = cur.next;
135+
cur.next = null;
136+
ListNode start = pre.next;
137+
pre.next = reverseList(start);
138+
start.next = t;
139+
pre = start;
140+
cur = pre;
141+
}
142+
return dummy.next;
143+
}
144+
145+
private ListNode reverseList(ListNode head) {
146+
ListNode pre = null, p = head;
147+
while (p != null) {
148+
ListNode q = p.next;
149+
p.next = pre;
150+
pre = p;
151+
p = q;
152+
}
153+
return pre;
154+
}
155+
}
156+
```
157+
158+
### **Go**
159+
160+
```go
161+
/**
162+
* Definition for singly-linked list.
163+
* type ListNode struct {
164+
* Val int
165+
* Next *ListNode
166+
* }
167+
*/
168+
func reverseKGroup(head *ListNode, k int) *ListNode {
169+
dummy := &ListNode{0, head}
170+
pre := dummy
171+
cur := dummy
172+
for cur.Next != nil {
173+
for i := 0; i < k && cur != nil; i++ {
174+
cur = cur.Next
175+
}
176+
if cur == nil {
177+
return dummy.Next
178+
}
179+
t := cur.Next
180+
cur.Next = nil
181+
start := pre.Next
182+
pre.Next = reverseList(start)
183+
start.Next = t
184+
pre = start
185+
cur = pre
186+
}
187+
return dummy.Next
188+
}
189+
190+
func reverseList(head *ListNode) *ListNode {
191+
if head == nil || head.Next == nil {
192+
return head
193+
}
194+
dummyHead := &ListNode{}
195+
cur := head
196+
for cur != nil {
197+
tmp := cur.Next
198+
cur.Next = dummyHead.Next
199+
dummyHead.Next = cur
200+
cur = tmp
201+
}
202+
return dummyHead.Next
203+
}
204+
```
84205

206+
### **C#**
207+
208+
```cs
209+
/**
210+
* Definition for singly-linked list.
211+
* public class ListNode {
212+
* public int val;
213+
* public ListNode next;
214+
* public ListNode(int val=0, ListNode next=null) {
215+
* this.val = val;
216+
* this.next = next;
217+
* }
218+
* }
219+
*/
220+
public class Solution {
221+
public ListNode ReverseKGroup(ListNode head, int k) {
222+
ListNode dummy = new ListNode(0, head);
223+
ListNode pre = dummy, cur = dummy;
224+
while (cur.next != null)
225+
{
226+
for (int i = 0; i < k && cur != null; ++i)
227+
{
228+
cur = cur.next;
229+
}
230+
if (cur == null)
231+
{
232+
return dummy.next;
233+
}
234+
ListNode t = cur.next;
235+
cur.next = null;
236+
ListNode start = pre.next;
237+
pre.next = ReverseList(start);
238+
start.next = t;
239+
pre = start;
240+
cur = pre;
241+
}
242+
return dummy.next;
243+
}
244+
245+
private ListNode ReverseList(ListNode head) {
246+
ListNode pre = null, p = head;
247+
while (p != null)
248+
{
249+
ListNode q = p.next;
250+
p.next = pre;
251+
pre = p;
252+
p = q;
253+
}
254+
return pre;
255+
}
256+
}
85257
```
86258

87259
### **...**

solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md

+174-2
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,193 @@
5454
<li><code>1 &lt;= k &lt;= sz</code></li>
5555
</ul>
5656

57-
5857
## Solutions
5958

6059
<!-- tabs:start -->
6160

6261
### **Python3**
6362

6463
```python
65-
64+
# Definition for singly-linked list.
65+
# class ListNode:
66+
# def __init__(self, val=0, next=None):
67+
# self.val = val
68+
# self.next = next
69+
class Solution:
70+
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
71+
def reverseList(head):
72+
pre, p = None, head
73+
while p:
74+
q = p.next
75+
p.next = pre
76+
pre = p
77+
p = q
78+
return pre
79+
80+
dummy = ListNode(next=head)
81+
pre = cur = dummy
82+
while cur.next:
83+
for _ in range(k):
84+
cur = cur.next
85+
if cur is None:
86+
return dummy.next
87+
t = cur.next
88+
cur.next = None
89+
start = pre.next
90+
pre.next = reverseList(start)
91+
start.next = t
92+
pre = start
93+
cur = pre
94+
return dummy.next
6695
```
6796

6897
### **Java**
6998

7099
```java
100+
/**
101+
* Definition for singly-linked list.
102+
* public class ListNode {
103+
* int val;
104+
* ListNode next;
105+
* ListNode() {}
106+
* ListNode(int val) { this.val = val; }
107+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
108+
* }
109+
*/
110+
class Solution {
111+
public ListNode reverseKGroup(ListNode head, int k) {
112+
ListNode dummy = new ListNode(0, head);
113+
ListNode pre = dummy, cur = dummy;
114+
while (cur.next != null) {
115+
for (int i = 0; i < k && cur != null; ++i) {
116+
cur = cur.next;
117+
}
118+
if (cur == null) {
119+
return dummy.next;
120+
}
121+
ListNode t = cur.next;
122+
cur.next = null;
123+
ListNode start = pre.next;
124+
pre.next = reverseList(start);
125+
start.next = t;
126+
pre = start;
127+
cur = pre;
128+
}
129+
return dummy.next;
130+
}
131+
132+
private ListNode reverseList(ListNode head) {
133+
ListNode pre = null, p = head;
134+
while (p != null) {
135+
ListNode q = p.next;
136+
p.next = pre;
137+
pre = p;
138+
p = q;
139+
}
140+
return pre;
141+
}
142+
}
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
/**
149+
* Definition for singly-linked list.
150+
* type ListNode struct {
151+
* Val int
152+
* Next *ListNode
153+
* }
154+
*/
155+
func reverseKGroup(head *ListNode, k int) *ListNode {
156+
dummy := &ListNode{0, head}
157+
pre := dummy
158+
cur := dummy
159+
for cur.Next != nil {
160+
for i := 0; i < k && cur != nil; i++ {
161+
cur = cur.Next
162+
}
163+
if cur == nil {
164+
return dummy.Next
165+
}
166+
t := cur.Next
167+
cur.Next = nil
168+
start := pre.Next
169+
pre.Next = reverseList(start)
170+
start.Next = t
171+
pre = start
172+
cur = pre
173+
}
174+
return dummy.Next
175+
}
176+
177+
func reverseList(head *ListNode) *ListNode {
178+
if head == nil ||head.Next == nil {
179+
return head
180+
}
181+
dummyHead := &ListNode{}
182+
cur := head
183+
for cur != nil {
184+
tmp := cur.Next
185+
cur.Next = dummyHead.Next
186+
dummyHead.Next = cur
187+
cur = tmp
188+
}
189+
return dummyHead.Next
190+
}
191+
```
71192

193+
### **C#**
194+
195+
```cs
196+
/**
197+
* Definition for singly-linked list.
198+
* public class ListNode {
199+
* public int val;
200+
* public ListNode next;
201+
* public ListNode(int val=0, ListNode next=null) {
202+
* this.val = val;
203+
* this.next = next;
204+
* }
205+
* }
206+
*/
207+
public class Solution {
208+
public ListNode ReverseKGroup(ListNode head, int k) {
209+
ListNode dummy = new ListNode(0, head);
210+
ListNode pre = dummy, cur = dummy;
211+
while (cur.next != null)
212+
{
213+
for (int i = 0; i < k && cur != null; ++i)
214+
{
215+
cur = cur.next;
216+
}
217+
if (cur == null)
218+
{
219+
return dummy.next;
220+
}
221+
ListNode t = cur.next;
222+
cur.next = null;
223+
ListNode start = pre.next;
224+
pre.next = ReverseList(start);
225+
start.next = t;
226+
pre = start;
227+
cur = pre;
228+
}
229+
return dummy.next;
230+
}
231+
232+
private ListNode ReverseList(ListNode head) {
233+
ListNode pre = null, p = head;
234+
while (p != null)
235+
{
236+
ListNode q = p.next;
237+
p.next = pre;
238+
pre = p;
239+
p = q;
240+
}
241+
return pre;
242+
}
243+
}
72244
```
73245

74246
### **...**

0 commit comments

Comments
 (0)