# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        nums=[]
        while head:
            nums.append(head)
            head=head.next
        n=len(nums)
        if n==0:
            return
        if k>n:
            k=k%n
        nums=nums[n-k:]+nums[:n-k]
        head=cur=ListNode(-1)
        for i in range(n):
            cur.next=nums[i]
            cur=cur.next
        cur.next=None
        return head.next