forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0061.py
More file actions
37 lines (28 loc) · 741 Bytes
/
0061.py
File metadata and controls
37 lines (28 loc) · 741 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
29
30
31
32
33
34
35
36
37
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head or not head.next or k == 0:
return head
len = 0
curr = head
while curr:
len += 1
curr = curr.next
k %= len
if k == 0:
return head
slow = head
fast = head
for _ in range(k):
fast = fast.next
while fast and fast.next:
slow = slow.next
fast = fast.next
ans = slow.next
slow.next = None
fast.next = head
return ans