# Definition for singly-linked list.classListNode:def__init__(self,x):self.val=xself.next=NoneclassSolution:# @param head: the list# @param k: rotate to the right k places# @return: the list after rotationdefrotateRight(self,head,k):# write your code hereifhead==Noneork==0orhead.next==None:returnheaddummy_node=ListNode(-1)dummy_node.next=headnew_head=dummy_node.nextnext_node=dummy_node.next# find the length of the listlen=1whilenext_node.next!=None:len+=1next_node=next_node.next# find the pospos=k%lenifpos==0:returnhead# find the new headwhilelen-pos>1:new_head=new_head.nextpos+=1result=new_head.nextnew_head.next=None# connect old tail with old headnext_node.next=dummy_node.nextreturnresultif__name__=="__main__":sol=Solution()node1=ListNode(1)node2=ListNode(2)node3=ListNode(3)node4=ListNode(2)node5=ListNode(1)node1.next=node2node2.next=node3node3.next=node4node4.next=node5sol.rotateRight(node1,1)