# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = NoneclassSolution(object):defrotateRight(self,head,k):ifhead==Noneorhead.next==None:returnheadl=1i=0tmp=head# Get the length whiletmp.next!=None:l+=1tmp=tmp.nextdummy_node=ListNode(-1)dummy_node.next=headk=k%lifk==0:returnheadwhilei<l-k:dummy_node=dummy_node.nexti+=1newhead=dummy_node.nextdummy_node.next=None# connect the old head to new head tmp=newheadwhiletmp.next!=None:tmp=tmp.nexttmp.next=headreturnnewhead