-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path61.旋转链表.py
More file actions
39 lines (30 loc) · 877 Bytes
/
61.旋转链表.py
File metadata and controls
39 lines (30 loc) · 877 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#
# @lc app=leetcode.cn id=61 lang=python3
#
# [61] 旋转链表
#
# @lc code=start
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head:
return
temp=head
n=1
# 计算链表长度
while temp.next:
temp=temp.next
n=n+1
k=k%n #实际需要从后往前移多少次
temp.next=head # 形成闭合环状链表
# 寻找移动后头节点位置
for i in range(n-k):
temp=temp.next
head=temp.next
temp.next=None # 断开环状链表
return head
# @lc code=end