-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate_right.py
45 lines (37 loc) · 1 KB
/
rotate_right.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
# 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: ListNode, k: int) -> ListNode:
if head is None:
return head
cur = head
count = 0
while cur:
count += 1
cur = cur.next
k %= count
if k == 0:
return head
k = -k
cur = cur1 = tail = before = head
while cur:
if k >= 0:
before = cur1
cur1 = cur1.next
k += 1
cur = cur.next
if cur is not None:
tail = cur
if before is None:
return cur1
tail.next = head
before.next = None
return cur1
if __name__ == "__main__":
r = Solution().rotateRight(ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))), 2)
while r:
print(r.val, end=" ")
r = r.next