# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if head==None:return []
cnt=1
last=head
while(last.next!=None):
last=last.next
cnt+=1
k%=cnt
if k==0:return head
curHead=head
last.next=head
print last.val
while(cnt>k+1):
cnt-=1
curHead=curHead.next
ans=curHead.next
curHead.next=None
return ans
本文介绍了一种实现单链表循环右移的方法。通过计算链表长度并对移动次数取模,实现高效循环右移。文章包含完整的Python代码示例。
255

被折叠的 条评论
为什么被折叠?



