-
Notifications
You must be signed in to change notification settings - Fork 92
/
177.py
51 lines (36 loc) · 971 Bytes
/
177.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
"""
Problem:
Given a linked list and a positive integer k, rotate the list to the right by k places.
For example, given the linked list 7 -> 7 -> 3 -> 5 and k = 2, it should become
3 -> 5 -> 7 -> 7.
Given the linked list 1 -> 2 -> 3 -> 4 -> 5 and k = 3, it should become
3 -> 4 -> 5 -> 1 -> 2.
"""
from DataStructures.LinkedList import Node, LinkedList
def rotate_linked_list(ll: LinkedList, k: int = 0) -> None:
k = k % ll.length
for _ in range(k):
temp = ll.head
ll.head = ll.head.next
temp.next = None
ll.rear.next = temp
ll.rear = ll.rear.next
if __name__ == "__main__":
LL = LinkedList()
for num in [7, 7, 3, 5]:
LL.add(num)
print(LL)
rotate_linked_list(LL, 2)
print(LL)
print()
LL = LinkedList()
for num in [1, 2, 3, 4, 5]:
LL.add(num)
print(LL)
rotate_linked_list(LL, 3)
print(LL)
"""
SPECS:
TIME COMPLEXITY: O(k)
SPACE COMPLEXITY: O(1)
"""