1
# Definition for singly-linked list.
2
# class ListNode:
3
# def __init__(self, val=0, next=None):
4
# self.val = val
5
# self.next = next
6
class Solution:
7
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
8
if k == 0 or head is None or head.next is None:
9
return head
10
cur = head
11
n = 0
12
while cur is not None:
13
cur = cur.next
14
n += 1
15
k = n - k % n
16
if k == n:
17
return head
18
cur = head
19
prev = None
20
while k > 0 and cur is not None:
21
prev = cur
22
cur = cur.next
23
k -= 1
24
prev.next = None
25
prev = cur
26
while cur.next is not None:
27
cur = cur.next
28
cur.next = head
29
return prev

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0