Skip to content

Commit 92dbea0

Browse files
authored
feat: update solutions to lc problem: No.0061 (doocs#1535)
No.0061.Rotate List
1 parent 8542eb4 commit 92dbea0

File tree

8 files changed

+456
-418
lines changed

8 files changed

+456
-418
lines changed

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

Lines changed: 137 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,19 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41-
将链表右半部分的 k 的节点拼接到 head 即可。
41+
**方法一:快慢指针 + 链表拼接**
4242

43-
注:k 对链表长度 n 取余,即 `k %= n`
43+
我们先判断链表节点数是否小于 $2$,如果是,直接返回 $head$ 即可。
44+
45+
否则,我们先统计链表节点数 $n$,然后将 $k$ 对 $n$ 取模,得到 $k$ 的有效值。
46+
47+
如果 $k$ 的有效值为 $0$,说明链表不需要旋转,直接返回 $head$ 即可。
48+
49+
否则,我们用快慢指针,让快指针先走 $k$ 步,然后快慢指针同时走,直到快指针走到链表尾部,此时慢指针的下一个节点就是新的链表头节点。
50+
51+
最后,我们将链表拼接起来即可。
52+
53+
时间复杂度 $O(n)$,其中 $n$ 是链表节点数,空间复杂度 $O(1)$。
4454

4555
<!-- tabs:start -->
4656

@@ -55,26 +65,26 @@
5565
# self.val = val
5666
# self.next = next
5767
class Solution:
58-
def rotateRight(self, head: ListNode, k: int) -> ListNode:
59-
if k == 0 or head is None or head.next is None:
68+
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
69+
if head is None or head.next is None:
6070
return head
61-
n, cur = 0, head
71+
cur, n = head, 0
6272
while cur:
63-
n, cur = n + 1, cur.next
73+
n += 1
74+
cur = cur.next
6475
k %= n
6576
if k == 0:
6677
return head
67-
68-
slow = fast = head
78+
fast = slow = head
6979
for _ in range(k):
7080
fast = fast.next
7181
while fast.next:
72-
slow, fast = slow.next, fast.next
82+
fast, slow = fast.next, slow.next
7383

74-
start = slow.next
84+
ans = slow.next
7585
slow.next = None
7686
fast.next = head
77-
return start
87+
return ans
7888
```
7989

8090
### **Java**
@@ -94,119 +104,32 @@ class Solution:
94104
*/
95105
class Solution {
96106
public ListNode rotateRight(ListNode head, int k) {
97-
if (k == 0 || head == null || head.next == null) {
107+
if (head == null || head.next == null) {
98108
return head;
99109
}
110+
ListNode cur = head;
100111
int n = 0;
101-
for (ListNode cur = head; cur != null; cur = cur.next) {
102-
++n;
112+
for (; cur != null; cur = cur.next) {
113+
n++;
103114
}
104115
k %= n;
105116
if (k == 0) {
106117
return head;
107118
}
108-
ListNode slow = head, fast = head;
119+
ListNode fast = head;
120+
ListNode slow = head;
109121
while (k-- > 0) {
110122
fast = fast.next;
111123
}
112124
while (fast.next != null) {
113-
slow = slow.next;
114125
fast = fast.next;
126+
slow = slow.next;
115127
}
116-
117-
ListNode start = slow.next;
128+
ListNode ans = slow.next;
118129
slow.next = null;
119130
fast.next = head;
120-
return start;
121-
}
122-
}
123-
```
124-
125-
### **TypeScript**
126-
127-
```ts
128-
/**
129-
* Definition for singly-linked list.
130-
* class ListNode {
131-
* val: number
132-
* next: ListNode | null
133-
* constructor(val?: number, next?: ListNode | null) {
134-
* this.val = (val===undefined ? 0 : val)
135-
* this.next = (next===undefined ? null : next)
136-
* }
137-
* }
138-
*/
139-
140-
function rotateRight(head: ListNode | null, k: number): ListNode | null {
141-
if (k == 0 || head == null || head.next == null) return head;
142-
// mod n
143-
let n = 0;
144-
let p = head;
145-
while (p != null) {
146-
++n;
147-
p = p.next;
148-
}
149-
k %= n;
150-
if (k == 0) return head;
151-
152-
let fast = head,
153-
slow = head;
154-
for (let i = 0; i < k; ++i) {
155-
fast = fast.next;
156-
}
157-
while (fast.next != null) {
158-
slow = slow.next;
159-
fast = fast.next;
160-
}
161-
let start = slow.next;
162-
slow.next = null;
163-
fast.next = head;
164-
return start;
165-
}
166-
```
167-
168-
```ts
169-
/**
170-
* Definition for singly-linked list.
171-
* class ListNode {
172-
* val: number
173-
* next: ListNode | null
174-
* constructor(val?: number, next?: ListNode | null) {
175-
* this.val = (val===undefined ? 0 : val)
176-
* this.next = (next===undefined ? null : next)
177-
* }
178-
* }
179-
*/
180-
181-
function rotateRight(head: ListNode | null, k: number): ListNode | null {
182-
if (head == null || k === 0) {
183-
return head;
184-
}
185-
186-
let n = 0;
187-
let cur = head;
188-
while (cur != null) {
189-
cur = cur.next;
190-
n++;
131+
return ans;
191132
}
192-
k = k % n;
193-
if (k === 0) {
194-
return head;
195-
}
196-
197-
cur = head;
198-
for (let i = 0; i < n - k - 1; i++) {
199-
cur = cur.next;
200-
}
201-
202-
const res = cur.next;
203-
cur.next = null;
204-
cur = res;
205-
while (cur.next != null) {
206-
cur = cur.next;
207-
}
208-
cur.next = head;
209-
return res;
210133
}
211134
```
212135

@@ -226,34 +149,120 @@ function rotateRight(head: ListNode | null, k: number): ListNode | null {
226149
class Solution {
227150
public:
228151
ListNode* rotateRight(ListNode* head, int k) {
229-
if (k == 0 || !head || !head->next) {
152+
if (!head || !head->next) {
230153
return head;
231154
}
155+
ListNode* cur = head;
232156
int n = 0;
233-
for (ListNode* cur = head; !!cur; cur = cur->next) {
157+
while (cur) {
234158
++n;
159+
cur = cur->next;
235160
}
236161
k %= n;
237162
if (k == 0) {
238163
return head;
239164
}
240-
ListNode *slow = head, *fast = head;
241-
while (k-- > 0) {
165+
ListNode* fast = head;
166+
ListNode* slow = head;
167+
while (k--) {
242168
fast = fast->next;
243169
}
244170
while (fast->next) {
245-
slow = slow->next;
246171
fast = fast->next;
172+
slow = slow->next;
247173
}
248-
249-
ListNode* start = slow->next;
174+
ListNode* ans = slow->next;
250175
slow->next = nullptr;
251176
fast->next = head;
252-
return start;
177+
return ans;
253178
}
254179
};
255180
```
256181
182+
### **Go**
183+
184+
```go
185+
/**
186+
* Definition for singly-linked list.
187+
* type ListNode struct {
188+
* Val int
189+
* Next *ListNode
190+
* }
191+
*/
192+
func rotateRight(head *ListNode, k int) *ListNode {
193+
if head == nil || head.Next == nil {
194+
return head
195+
}
196+
cur := head
197+
n := 0
198+
for cur != nil {
199+
cur = cur.Next
200+
n++
201+
}
202+
k %= n
203+
if k == 0 {
204+
return head
205+
}
206+
fast, slow := head, head
207+
for i := 0; i < k; i++ {
208+
fast = fast.Next
209+
}
210+
for fast.Next != nil {
211+
fast = fast.Next
212+
slow = slow.Next
213+
}
214+
ans := slow.Next
215+
slow.Next = nil
216+
fast.Next = head
217+
return ans
218+
}
219+
```
220+
221+
### **TypeScript**
222+
223+
```ts
224+
/**
225+
* Definition for singly-linked list.
226+
* class ListNode {
227+
* val: number
228+
* next: ListNode | null
229+
* constructor(val?: number, next?: ListNode | null) {
230+
* this.val = (val===undefined ? 0 : val)
231+
* this.next = (next===undefined ? null : next)
232+
* }
233+
* }
234+
*/
235+
236+
function rotateRight(head: ListNode | null, k: number): ListNode | null {
237+
if (!head || !head.next) {
238+
return head;
239+
}
240+
let cur = head;
241+
let n = 0;
242+
while (cur) {
243+
cur = cur.next;
244+
++n;
245+
}
246+
k %= n;
247+
if (k === 0) {
248+
return head;
249+
}
250+
let fast = head;
251+
let slow = head;
252+
while (k--) {
253+
fast = fast.next;
254+
}
255+
while (fast.next) {
256+
fast = fast.next;
257+
slow = slow.next;
258+
}
259+
const ans = slow.next;
260+
slow.next = null;
261+
fast.next = head;
262+
return ans;
263+
}
264+
```
265+
257266
### **C#**
258267

259268
```cs
@@ -270,35 +279,32 @@ public:
270279
*/
271280
public class Solution {
272281
public ListNode RotateRight(ListNode head, int k) {
273-
if (k == 0 || head == null || head.next == null)
274-
{
282+
if (head == null || head.next == null) {
275283
return head;
276284
}
277-
var n = 0;
278-
for (ListNode cur = head; cur != null; cur = cur.next)
279-
{
285+
var cur = head;
286+
int n = 0;
287+
while (cur != null) {
288+
cur = cur.next;
280289
++n;
281290
}
282291
k %= n;
283-
if (k == 0)
284-
{
292+
if (k == 0) {
285293
return head;
286294
}
287-
ListNode slow = head, fast = head;
288-
while (k-- > 0)
289-
{
295+
var fast = head;
296+
var slow = head;
297+
while (k-- > 0) {
290298
fast = fast.next;
291299
}
292-
while (fast.next != null)
293-
{
294-
slow = slow.next;
300+
while (fast.next != null) {
295301
fast = fast.next;
302+
slow = slow.next;
296303
}
297-
298-
ListNode start = slow.next;
304+
var ans = slow.next;
299305
slow.next = null;
300306
fast.next = head;
301-
return start;
307+
return ans;
302308
}
303309
}
304310
```

0 commit comments

Comments
 (0)