Skip to content

Commit b56d67f

Browse files
committed
feat: update solutions to lc problem: No.0061. Rotate List
1 parent 0aaa6e9 commit b56d67f

File tree

6 files changed

+217
-93
lines changed

6 files changed

+217
-93
lines changed

solution/0000-0099/0061.Rotate List/README.md

+74-31
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ class Solution:
6565
k %= n
6666
if k == 0:
6767
return head
68-
p = q = head
69-
for i in range(k):
70-
q = q.next
71-
while q.next:
72-
p, q = p.next, q.next
73-
start = p.next
74-
p.next = None
75-
q.next = head
68+
69+
slow = fast = head
70+
for _ in range(k):
71+
fast = fast.next
72+
while fast.next:
73+
slow, fast = slow.next, fast.next
74+
75+
start = slow.next
76+
slow.next = None
77+
fast.next = head
7678
return start
7779
```
7880

@@ -96,27 +98,26 @@ class Solution {
9698
if (k == 0 || head == null || head.next == null) {
9799
return head;
98100
}
99-
ListNode cur = head;
100101
int n = 0;
101-
while (cur != null) {
102-
cur = cur.next;
102+
for (ListNode cur = head; cur != null; cur = cur.next) {
103103
++n;
104104
}
105105
k %= n;
106106
if (k == 0) {
107107
return head;
108108
}
109-
ListNode p = head, q = head;
109+
ListNode slow = head, fast = head;
110110
while (k-- > 0) {
111-
q = q.next;
111+
fast = fast.next;
112112
}
113-
while (q.next != null) {
114-
p = p.next;
115-
q = q.next;
113+
while (fast.next != null) {
114+
slow = slow.next;
115+
fast = fast.next;
116116
}
117-
ListNode start = p.next;
118-
p.next = null;
119-
q.next = head;
117+
118+
ListNode start = slow.next;
119+
slow.next = null;
120+
fast.next = head;
120121
return start;
121122
}
122123
}
@@ -164,6 +165,50 @@ function rotateRight(head: ListNode | null, k: number): ListNode | null {
164165
};
165166
```
166167

168+
### **C++**
169+
170+
```cpp
171+
/**
172+
* Definition for singly-linked list.
173+
* struct ListNode {
174+
* int val;
175+
* ListNode *next;
176+
* ListNode() : val(0), next(nullptr) {}
177+
* ListNode(int x) : val(x), next(nullptr) {}
178+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
179+
* };
180+
*/
181+
class Solution {
182+
public:
183+
ListNode* rotateRight(ListNode* head, int k) {
184+
if (k == 0 || !head || !head->next) {
185+
return head;
186+
}
187+
int n = 0;
188+
for (ListNode *cur = head; !!cur; cur = cur->next) {
189+
++n;
190+
}
191+
k %= n;
192+
if (k == 0) {
193+
return head;
194+
}
195+
ListNode *slow = head, *fast = head;
196+
while (k-- > 0) {
197+
fast = fast->next;
198+
}
199+
while (fast->next) {
200+
slow = slow->next;
201+
fast = fast->next;
202+
}
203+
204+
ListNode *start = slow->next;
205+
slow->next = nullptr;
206+
fast->next = head;
207+
return start;
208+
}
209+
};
210+
```
211+
167212
### **C#**
168213

169214
```cs
@@ -184,33 +229,31 @@ public class Solution {
184229
{
185230
return head;
186231
}
187-
ListNode cur = head;
188232
var n = 0;
189-
while (cur != null)
233+
for (ListNode cur = head; cur != null; cur = cur.next)
190234
{
191-
cur = cur.next;
192235
++n;
193236
}
194237
k %= n;
195238
if (k == 0)
196239
{
197240
return head;
198241
}
199-
ListNode p = head, q = head;
242+
ListNode slow = head, fast = head;
200243
while (k-- > 0)
201244
{
202-
q = q.next;
245+
fast = fast.next;
203246
}
204-
while (q.next != null)
247+
while (fast.next != null)
205248
{
206-
p = p.next;
207-
q = q.next;
249+
slow = slow.next;
250+
fast = fast.next;
208251
}
209-
ListNode start = p.next;
210-
p.next = null;
211-
q.next = head;
212-
return start;
213252

253+
ListNode start = slow.next;
254+
slow.next = null;
255+
fast.next = head;
256+
return start;
214257
}
215258
}
216259
```

solution/0000-0099/0061.Rotate List/README_EN.md

+74-31
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ class Solution:
5353
k %= n
5454
if k == 0:
5555
return head
56-
p = q = head
57-
for i in range(k):
58-
q = q.next
59-
while q.next:
60-
p, q = p.next, q.next
61-
start = p.next
62-
p.next = None
63-
q.next = head
56+
57+
slow = fast = head
58+
for _ in range(k):
59+
fast = fast.next
60+
while fast.next:
61+
slow, fast = slow.next, fast.next
62+
63+
start = slow.next
64+
slow.next = None
65+
fast.next = head
6466
return start
6567
```
6668

@@ -82,27 +84,26 @@ class Solution {
8284
if (k == 0 || head == null || head.next == null) {
8385
return head;
8486
}
85-
ListNode cur = head;
8687
int n = 0;
87-
while (cur != null) {
88-
cur = cur.next;
88+
for (ListNode cur = head; cur != null; cur = cur.next) {
8989
++n;
9090
}
9191
k %= n;
9292
if (k == 0) {
9393
return head;
9494
}
95-
ListNode p = head, q = head;
95+
ListNode slow = head, fast = head;
9696
while (k-- > 0) {
97-
q = q.next;
97+
fast = fast.next;
9898
}
99-
while (q.next != null) {
100-
p = p.next;
101-
q = q.next;
99+
while (fast.next != null) {
100+
slow = slow.next;
101+
fast = fast.next;
102102
}
103-
ListNode start = p.next;
104-
p.next = null;
105-
q.next = head;
103+
104+
ListNode start = slow.next;
105+
slow.next = null;
106+
fast.next = head;
106107
return start;
107108
}
108109
}
@@ -150,6 +151,50 @@ function rotateRight(head: ListNode | null, k: number): ListNode | null {
150151
};
151152
```
152153

154+
### **C++**
155+
156+
```cpp
157+
/**
158+
* Definition for singly-linked list.
159+
* struct ListNode {
160+
* int val;
161+
* ListNode *next;
162+
* ListNode() : val(0), next(nullptr) {}
163+
* ListNode(int x) : val(x), next(nullptr) {}
164+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
165+
* };
166+
*/
167+
class Solution {
168+
public:
169+
ListNode* rotateRight(ListNode* head, int k) {
170+
if (k == 0 || !head || !head->next) {
171+
return head;
172+
}
173+
int n = 0;
174+
for (ListNode *cur = head; !!cur; cur = cur->next) {
175+
++n;
176+
}
177+
k %= n;
178+
if (k == 0) {
179+
return head;
180+
}
181+
ListNode *slow = head, *fast = head;
182+
while (k-- > 0) {
183+
fast = fast->next;
184+
}
185+
while (fast->next) {
186+
slow = slow->next;
187+
fast = fast->next;
188+
}
189+
190+
ListNode *start = slow->next;
191+
slow->next = nullptr;
192+
fast->next = head;
193+
return start;
194+
}
195+
};
196+
```
197+
153198
### **C#**
154199

155200
```cs
@@ -170,33 +215,31 @@ public class Solution {
170215
{
171216
return head;
172217
}
173-
ListNode cur = head;
174218
var n = 0;
175-
while (cur != null)
219+
for (ListNode cur = head; cur != null; cur = cur.next)
176220
{
177-
cur = cur.next;
178221
++n;
179222
}
180223
k %= n;
181224
if (k == 0)
182225
{
183226
return head;
184227
}
185-
ListNode p = head, q = head;
228+
ListNode slow = head, fast = head;
186229
while (k-- > 0)
187230
{
188-
q = q.next;
231+
fast = fast.next;
189232
}
190-
while (q.next != null)
233+
while (fast.next != null)
191234
{
192-
p = p.next;
193-
q = q.next;
235+
slow = slow.next;
236+
fast = fast.next;
194237
}
195-
ListNode start = p.next;
196-
p.next = null;
197-
q.next = head;
198-
return start;
199238

239+
ListNode start = slow.next;
240+
slow.next = null;
241+
fast.next = head;
242+
return start;
200243
}
201244
}
202245
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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* rotateRight(ListNode* head, int k) {
14+
if (k == 0 || !head || !head->next) {
15+
return head;
16+
}
17+
int n = 0;
18+
for (ListNode *cur = head; !!cur; cur = cur->next) {
19+
++n;
20+
}
21+
k %= n;
22+
if (k == 0) {
23+
return head;
24+
}
25+
ListNode *slow = head, *fast = head;
26+
while (k-- > 0) {
27+
fast = fast->next;
28+
}
29+
while (fast->next) {
30+
slow = slow->next;
31+
fast = fast->next;
32+
}
33+
34+
ListNode *start = slow->next;
35+
slow->next = nullptr;
36+
fast->next = head;
37+
return start;
38+
}
39+
};

0 commit comments

Comments
 (0)