-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path61-rotate-list.py
38 lines (35 loc) · 1012 Bytes
/
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
37
38
# 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 not head.next:
return head
prev, cur, old_head = None, head, head
count = 0
while cur:
count += 1
prev = cur
cur = cur.next
old_tail = prev
k %= count
if k == 0:
return head
step = k + 1
dummy = prev = cur = ListNode(next=head)
while step:
cur = cur.next
step -= 1
while cur:
prev = prev.next
cur = cur.next
new_tail = prev
new_head = prev.next
old_tail.next = old_head
new_tail.next = None
return new_head
# time O(n)
# space O(1)
# using linked list and counting length and two pointers