-
Notifications
You must be signed in to change notification settings - Fork 43
/
reverse-nodes-in-k-group.py
401 lines (330 loc) · 11.2 KB
/
reverse-nodes-in-k-group.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
"""
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is.
You may not alter the values in the list's nodes, only nodes themselves may be changed.
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Example 2:
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
Constraints:
The number of nodes in the list is n.
1 <= k <= n <= 5000
0 <= Node.val <= 1000
Follow-up: Can you solve the problem in O(1) extra memory space?
"""
# V0
# IDEA : Iterative
class Solution:
def reverseKGroup(self, head, k):
# help func
# check if # of sub nodes still > k
def check(head, k):
ans = 0
while head:
ans += 1
if ans >= k:
return True
head = head.next
return False
# edge case
if not head:
return
d = dummy = ListNode(None)
pre = None
preHead = curHead = head
while check(curHead, k):
for _ in range(k):
# reverse linked list
tmp = curHead.next
curHead.next = pre
pre = curHead
curHead = tmp
# reverse linked list
# ???
dummy.next = pre
dummy = preHead
preHead.next = curHead
preHead = curHead
return d.next
# V0'
# https://github.com/yennanliu/CS_basics/blob/master/doc/cheatsheet/linked_list.md#1-1-6-reverse-nodes-in-k-group--linked-list-iteration
# V1
# IDEA : Recursion
# https://leetcode.com/problems/reverse-nodes-in-k-group/solution/
class Solution:
def reverseLinkedList(self, head, k):
# Reverse k nodes of the given linked list.
# This function assumes that the list contains
# atleast k nodes.
new_head, ptr = None, head
while k:
# Keep track of the next node to process in the
# original list
next_node = ptr.next
# Insert the node pointed to by "ptr"
# at the beginning of the reversed list
ptr.next = new_head
new_head = ptr
# Move on to the next node
ptr = next_node
# Decrement the count of nodes to be reversed by 1
k -= 1
# Return the head of the reversed list
return new_head
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
count = 0
ptr = head
# First, see if there are atleast k nodes
# left in the linked list.
while count < k and ptr:
ptr = ptr.next
count += 1
# If we have k nodes, then we reverse them
if count == k:
# Reverse the first k nodes of the list and
# get the reversed list's head.
reversedHead = self.reverseLinkedList(head, k)
# Now recurse on the remaining linked list. Since
# our recursion returns the head of the overall processed
# list, we use that and the "original" head of the "k" nodes
# to re-wire the connections.
head.next = self.reverseKGroup(ptr, k)
return reversedHead
return head
# V1'
# IDEA : Iterative O(1) space
# https://leetcode.com/problems/reverse-nodes-in-k-group/solution/
class Solution:
def reverseLinkedList(self, head, k):
# Reverse k nodes of the given linked list.
# This function assumes that the list contains
# atleast k nodes.
new_head, ptr = None, head
while k:
# Keep track of the next node to process in the
# original list
next_node = ptr.next
# Insert the node pointed to by "ptr"
# at the beginning of the reversed list
ptr.next = new_head
new_head = ptr
# Move on to the next node
ptr = next_node
# Decrement the count of nodes to be reversed by 1
k -= 1
# Return the head of the reversed list
return new_head
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
ptr = head
ktail = None
# Head of the final, moified linked list
new_head = None
# Keep going until there are nodes in the list
while ptr:
count = 0
# Start counting nodes from the head
ptr = head
# Find the head of the next k nodes
while count < k and ptr:
ptr = ptr.next
count += 1
# If we counted k nodes, reverse them
if count == k:
# Reverse k nodes and get the new head
revHead = self.reverseLinkedList(head, k)
# new_head is the head of the final linked list
if not new_head:
new_head = revHead
# ktail is the tail of the previous block of
# reversed k nodes
if ktail:
ktail.next = revHead
ktail = head
head = ptr
# attach the final, possibly un-reversed portion
if ktail:
ktail.next = head
return new_head if new_head else head
# V1''
# IDEA : ITERATION + help func
# https://leetcode.com/problems/reverse-nodes-in-k-group/discuss/462808/Python-Clear-Solution
class Solution:
def reverseKGroup(self, head, k):
dummy = ListNode(None)
dummy.next = head
d = dummy
pre = None
curHead = head
preHead = curHead
while self.check(curHead, k):
for _ in range(k):
temp = curHead.next
curHead.next = pre
pre = curHead
curHead = temp
dummy.next = pre
dummy = preHead
preHead.next = curHead
preHead = curHead
return d.next
def check(self, head, k):
ans = 0
while head:
ans += 1
if ans >= k:
return True
head = head.next
return False
# V1
# https://zxi.mytechroad.com/blog/list/leetcode-25-reverse-nodes-in-k-group/
# C++
# class Solution {
# public:
# ListNode *reverseKGroup(ListNode *head, int k) {
# if (!head || k == 1) return head;
# ListNode dummy(0);
# dummy.next = head;
# int len = 1;
# while (head = head->next) len++;
# ListNode* pre = &dummy;
# for (int l = 0; l + k <= len; l += k) {
# ListNode* cur = pre->next;
# ListNode* nxt = cur->next;
# for (int i = 1; i < k; ++i) {
# cur->next = nxt->next;
# nxt->next = pre->next;
# pre->next = nxt;
# nxt = cur->next;
# }
# pre = cur;
# }
# return dummy.next;
# }
# };
# V1'''
# IDEA : RECURSIVE
# https://leetcode.com/problems/reverse-nodes-in-k-group/discuss/11676/64ms-python-solution1
class Solution(object):
def reverseKGroup(self, head, k):
if not head or not head.next:
return head
tail = head
for i in range(k):
if not tail:
return head
tail = tail.next
tail = self.reverseKGroup(tail, k)
for i in range(k):
next = head.next
head.next = tail
tail = head
head = next
return tail
# V1'''''
# https://leetcode.com/problems/reverse-nodes-in-k-group/discuss/11508/Python-solution-with-detailed-explanation
class Solution(object):
def reverseKGroup(self, head, K):
"""
:type head: ListNode
:rtype: ListNode
"""
splits = [ListNode(-1) for _ in range(K)]
tails = [splits[i] for i in range(K)]
k = 0
while head:
temp = head.next
tails[k % K].next = head
tails[k % K] = head
head.next = None
head = temp
k = k + 1
start, tails = ListNode(-1), [splits[i].next for i in range(K)]
result = start
while k > 0:
(s, e, inc) = (len(tails)-1,-1,-1) if tails[-1] else (0, len(tails), 1)
for i in range(s, e, inc):
if tails[i] != None:
temp = tails[i].next
result.next = tails[i]
result = tails[i]
tails[i].next = None
tails[i] = temp
k -= 1
return start.next
# V1'''''''
# https://leetcode.com/problems/reverse-nodes-in-k-group/discuss/211534/Python-solution
class Solution(object):
def reverseKGroup(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
while not head or not head.next:
return head
if k == 1:
return head
dummy = ListNode(0)
dummy.next = head
record = dummy
left = head
right = head.next
count = 1
while True:
while right and count < k: # reverse links within a group
tmp = right.next
right.next = left
prev = left
left = right
right = tmp
count += 1
if count == k: # rotate the group
tmp = dummy.next
dummy.next = left
tmp.next = right
dummy = tmp
count = 1
left = right
if right:
right = right.next
else:
if count >= 2:
left.next = None
if count > 2: # the last group has size < k, and we need to reverse the links within the group again.
while True:
tmp = prev.next
prev.next = left
if tmp.next == prev:
return record.next
left = prev
prev = tmp
return record.next
# V1''''''''
# https://zxi.mytechroad.com/blog/list/leetcode-25-reverse-nodes-in-k-group/
# C++
# class Solution {
# public:
# ListNode *reverseKGroup(ListNode *head, int k) {
# if (!head || k == 1) return head;
# ListNode dummy(0);
# dummy.next = head;
# int len = 1;
# while (head = head->next) len++;
# ListNode* pre = &dummy;
# for (int l = 0; l + k <= len; l += k) {
# ListNode* cur = pre->next;
# ListNode* nxt = cur->next;
# for (int i = 1; i < k; ++i) {
# cur->next = nxt->next;
# nxt->next = pre->next;
# pre->next = nxt;
# nxt = cur->next;
# }
# pre = cur;
# }
# return dummy.next;
# }
# };
# V2