-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path61_Rotate_List.py
36 lines (32 loc) · 1.25 KB
/
61_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
# 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
# k might greater than length of head.
# it is a waste to keep rotate the head if the linked list has been rotated for a full round.
# so we wanna tinker k so that we dont need to rotate over and over, but the result is still the same.
# Since we know every length of head times, the linked list is restore to the original list.
# Hence we change K to K%(length of head)
node = ListNode(0,head)
length = 0
while head:
head=head.next
length+=1
k = k%length
head= node.next
# only rotate the list for one step, and keep rotate until k is equal to 0.
while k>0:
node = changing = ListNode()
node.next = head
while head.next:
changing= changing.next
head=head.next
changing.next = head.next
head.next = node.next
k-=1
return head