-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0061_Rotate_List.py
42 lines (34 loc) · 1.04 KB
/
0061_Rotate_List.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
# 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:
return head
if not (head.next):
return head
else:
head_pointer = head.next
head_size = 1
dummy = ListNode(val=0, next=head)
last_pointer = dummy.next
while last_pointer.next:
head_size += 1
last_pointer = last_pointer.next
k = k % head_size
if k == 0:
return head
k = head_size - k
head_pointer = dummy.next
cut_pointer = head_pointer
while not k == 0:
k -= 1
if k == 0:
head_pointer = cut_pointer.next
cut_pointer.next = None
break
cut_pointer = cut_pointer.next
last_pointer.next = dummy.next
return head_pointer