题目
旋转链表 题目链接 官方题解
关键词
链表
代码记录
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
转载请注明原文地址: https://lol.8miu.com/read-5415.html