# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next classSolution: defrotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: size = 0 current_node = end = head while current_node: end = current_node current_node = current_node.next size += 1 if size <= 1: return head k %= size if k == 0: return head current_node = head for _ inrange(size - k - 1): current_node = current_node.next new_head = current_node.next current_node.next = None end.next = head return new_head