Medium
Given the head
of a linked list, rotate the list to the right by k
places.
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
Example 2:
Input: head = [0,1,2], k = 4
Output: [2,0,1]
Constraints:
[0, 500]
.-100 <= Node.val <= 100
0 <= k <= 2 * 109
from typing import Optional
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 head is None or k == 0:
return head
tail = head
# find the count and let tail points to last node
count = 1
while tail is not None and tail.next is not None:
count += 1
tail = tail.next
# calculate number of times to rotate by count modulas
times = k % count
if times == 0:
return head
temp = head
# iterate and go to the K+1 th node from the end or count - K - 1 node from start
for i in range(1, count - times):
if temp is not None:
temp = temp.next
new_head = None
if temp is not None and tail is not None:
new_head = temp.next
temp.next = None
tail.next = head
return new_head