-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14-Optimal.py
More file actions
28 lines (23 loc) · 838 Bytes
/
14-Optimal.py
File metadata and controls
28 lines (23 loc) · 838 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
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head:
return
if head and not head.next:
return head
if k == 0:
return head # Time saving checks
curr = head
tail = None
size = 0
while curr:
tail = curr
curr = curr.next
size += 1 # Calculating size
if k%size == 0: # Time saving
return head
tail.next = head # Making cyclic to ease process
curr = head
for i in range(2*size - k%size - 1): # Which Node will become TAIL after these many rotations
curr = curr.next
ans = curr.next # Mark Head
curr.next = None # Un-Cycle the list
return ans