61. 旋转链表(Medium)
Python示例
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head or not head.next:
return head
n = 0
p = head
while p:
if p and not p.next:
end = p
n += 1
p = p.next
end.next = head # 连成环
k = k % n
p = head
for _ in range(n - k - 1): # n - k 个
p = p.next
res = p.next
p.next = None
return res