任务描述
思路
- 先成环,再断开
代码实现
# 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 or head.next == None or k == 0:
return head
tail = cur =head
l3 = cur
count = 1
while tail.next:
tail = tail.next
count += 1
tail.next = cur
for i in range(1,count-k%count):
cur = cur.next
l3 = cur.next
cur.next = None
return l3