-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0061.py
34 lines (29 loc) · 958 Bytes
/
0061.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head or head.next == None or k == 0:
return head
runs = 0
depth = 1
while runs < k:
node = head
prev = None
while True:
if not node.next:
prev.next = None
node.next = head
break
else:
if runs == 0: depth += 1
prev = node
node = node.next
runs += 1
if k > depth:
mod = k % depth
k = depth if mod == 0 else mod
head = node
return node