-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist_reverseNodesInKGroups.py
More file actions
36 lines (29 loc) · 1.14 KB
/
linkedlist_reverseNodesInKGroups.py
File metadata and controls
36 lines (29 loc) · 1.14 KB
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
"""
Given a linked list l, reverse its nodes k at a time and return the modified list.
k is a positive integer that is less than or equal to the length of l.
If the number of nodes in the linked list is not a multiple of k,
then the nodes that are left out at the end should remain as-is.
You may not alter the values in the nodes - only the nodes themselves can be changed.
Example
For l = [1, 2, 3, 4, 5] and k = 2, the output should be
reverseNodesInKGroups(l, k) = [2, 1, 4, 3, 5];
For l = [1, 2, 3, 4, 5] and k = 1, the output should be
reverseNodesInKGroups(l, k) = [1, 2, 3, 4, 5];
For l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] and k = 3, the output should be
reverseNodesInKGroups(l, k) = [3, 2, 1, 6, 5, 4, 9, 8, 7, 10, 11].
"""
def reverseNodesInKGroups(l, k):
if not l or k <= 1:
return l
new = []
while l:
new.append(l)
l = l.next
for i in range(len(new) // k):
start = k * i
end = k * (i + 1)
new = new[:start] + list(reversed(new[start:end])) + new[end:]
for i in range(len(new) - 1):
new[i].next = new[i+1]
new[-1].next = None
return new[0]