-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathProblem#177#699.py
More file actions
33 lines (30 loc) · 784 Bytes
/
Problem#177#699.py
File metadata and controls
33 lines (30 loc) · 784 Bytes
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
"""
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.
"""
def rotateRight(head, k):
if not head:
return
l = length(head)
k = k % l
if l == 1 or k % l == 0:
return head
count = 0
curr = head
while count < l-k:
prev = head
head = head.next
count += 1
prev.next = None
result = head
while head.next:
head = head.next
head.next = curr
return result
def length(head):
count = 0
while head:
count += 1
head = head.next
return count