将链表的首尾相连,然后新链表的头结点,断开即可
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if head is None or head.next is None:
return head
cur = head
n = 1
while cur.next:
cur = cur.next
n = n + 1
cur.next = head
count = n - k % n
while count > 0:
cur = cur.next
count = count - 1
newNode = cur.next
cur.next = None
return newNode