题目
关键词
链表
代码记录
# 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 not head:
return head
size=0
cur=head
tail=head
while cur:
size += 1
tail = cur
cur=cur.next
to_move= k%size
if to_move==0:
return head
split_tail=head
cnt=size-to_move-1
while cnt>0:
cnt -= 1
split_tail=split_tail.next
split_head=split_tail.next
tail.next=head
split_tail.next=None
return split_head