题目标题

链表把前n-k个元素移动到尾部

难度:初级

数据分析
参考解析

class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head or not head.next or not k: return head
tail, count= head, 1
while tail.next:
tail = tail.next
count += 1
k = k % count
if k == 0:
return head
newTail = newHead = head
for i in range(count - k):
newHead = newHead.next
for j in range(count - k - 1):
newTail = newTail.next
newTail.next = None
tail.next = head
return newHead