1# Definition for singly-linked list.2# class ListNode:3# def __init__(self, val=0, next=None):4# self.val = val5# self.next = next6class Solution:7def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:8if k == 0 or head is None or head.next is None:9return head10cur = head11n = 012while cur is not None:13cur = cur.next14n += 115k = n - k % n16if k == n:17return head18cur = head19prev = None20while k > 0 and cur is not None:21prev = cur22cur = cur.next23k -= 124prev.next = None25prev = cur26while cur.next is not None:27cur = cur.next28cur.next = head29return prev