# Given a list, rotate the list to the right by k places, where k is non-negative.## Example:# Given 1->2->3->4->5->NULL and k = 2,# return 4->5->1->2->3->NULL.
AC
classListNode:def__init__(self, x):
self.val = x
self.next = NoneclassSolution():defrotateRight(self, head, k):ifnot head:
returnNone
cur, n = head, 1while cur.next:
cur, n = cur.next, n+1
cur.next = head
tail = cur = head
for _ in range(n - k % n):
tail, cur = cur, cur.next
tail.next = Nonereturn cur
if __name__ == "__main__":
head, head.next, head.next.next, head.next.next.next, head.next.next.next.next \
= ListNode(1), ListNode(2), ListNode(3), ListNode(4), ListNode(5)
print(Solution().rotateRight(head, 2))