38
38
39
39
<!-- 这里可写通用的实现逻辑 -->
40
40
41
- 将链表右半部分的 k 的节点拼接到 head 即可。
41
+ ** 方法一:快慢指针 + 链表拼接 **
42
42
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)$。
44
54
45
55
<!-- tabs:start -->
46
56
55
65
# self.val = val
56
66
# self.next = next
57
67
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 :
60
70
return head
61
- n, cur = 0 , head
71
+ cur, n = head, 0
62
72
while cur:
63
- n, cur = n + 1 , cur.next
73
+ n += 1
74
+ cur = cur.next
64
75
k %= n
65
76
if k == 0 :
66
77
return head
67
-
68
- slow = fast = head
78
+ fast = slow = head
69
79
for _ in range (k):
70
80
fast = fast.next
71
81
while fast.next:
72
- slow, fast = slow .next, fast .next
82
+ fast, slow = fast .next, slow .next
73
83
74
- start = slow.next
84
+ ans = slow.next
75
85
slow.next = None
76
86
fast.next = head
77
- return start
87
+ return ans
78
88
```
79
89
80
90
### ** Java**
@@ -94,119 +104,32 @@ class Solution:
94
104
*/
95
105
class Solution {
96
106
public ListNode rotateRight (ListNode head , int k ) {
97
- if (k == 0 || head == null || head. next == null ) {
107
+ if (head == null || head. next == null ) {
98
108
return head;
99
109
}
110
+ ListNode cur = head;
100
111
int n = 0 ;
101
- for (ListNode cur = head ; cur != null ; cur = cur. next) {
102
- ++ n ;
112
+ for (; cur != null ; cur = cur. next) {
113
+ n ++ ;
103
114
}
104
115
k %= n;
105
116
if (k == 0 ) {
106
117
return head;
107
118
}
108
- ListNode slow = head, fast = head;
119
+ ListNode fast = head;
120
+ ListNode slow = head;
109
121
while (k-- > 0 ) {
110
122
fast = fast. next;
111
123
}
112
124
while (fast. next != null ) {
113
- slow = slow. next;
114
125
fast = fast. next;
126
+ slow = slow. next;
115
127
}
116
-
117
- ListNode start = slow. next;
128
+ ListNode ans = slow. next;
118
129
slow. next = null ;
119
130
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;
191
132
}
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 ;
210
133
}
211
134
```
212
135
@@ -226,34 +149,120 @@ function rotateRight(head: ListNode | null, k: number): ListNode | null {
226
149
class Solution {
227
150
public:
228
151
ListNode* rotateRight(ListNode* head, int k) {
229
- if (k == 0 || !head || !head->next) {
152
+ if (!head || !head->next) {
230
153
return head;
231
154
}
155
+ ListNode* cur = head;
232
156
int n = 0;
233
- for (ListNode * cur = head; !!cur; cur = cur->next ) {
157
+ while ( cur) {
234
158
++n;
159
+ cur = cur->next;
235
160
}
236
161
k %= n;
237
162
if (k == 0) {
238
163
return head;
239
164
}
240
- ListNode * slow = head, * fast = head;
241
- while (k-- > 0) {
165
+ ListNode* fast = head;
166
+ ListNode* slow = head;
167
+ while (k--) {
242
168
fast = fast->next;
243
169
}
244
170
while (fast->next) {
245
- slow = slow->next;
246
171
fast = fast->next;
172
+ slow = slow->next;
247
173
}
248
-
249
- ListNode* start = slow->next;
174
+ ListNode* ans = slow->next;
250
175
slow->next = nullptr;
251
176
fast->next = head;
252
- return start ;
177
+ return ans ;
253
178
}
254
179
};
255
180
```
256
181
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
+
257
266
### ** C#**
258
267
259
268
``` cs
@@ -270,35 +279,32 @@ public:
270
279
*/
271
280
public class Solution {
272
281
public ListNode RotateRight (ListNode head , int k ) {
273
- if (k == 0 || head == null || head .next == null )
274
- {
282
+ if (head == null || head .next == null ) {
275
283
return head ;
276
284
}
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 ;
280
289
++ n ;
281
290
}
282
291
k %= n ;
283
- if (k == 0 )
284
- {
292
+ if (k == 0 ) {
285
293
return head ;
286
294
}
287
- ListNode slow = head , fast = head ;
288
- while ( k -- > 0 )
289
- {
295
+ var fast = head ;
296
+ var slow = head ;
297
+ while ( k -- > 0 ) {
290
298
fast = fast .next ;
291
299
}
292
- while (fast .next != null )
293
- {
294
- slow = slow .next ;
300
+ while (fast .next != null ) {
295
301
fast = fast .next ;
302
+ slow = slow .next ;
296
303
}
297
-
298
- ListNode start = slow .next ;
304
+ var ans = slow .next ;
299
305
slow .next = null ;
300
306
fast .next = head ;
301
- return start ;
307
+ return ans ;
302
308
}
303
309
}
304
310
```
0 commit comments