-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path061.旋转链表.py
37 lines (33 loc) · 1001 Bytes
/
061.旋转链表.py
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
# 方法一:将倒数k个结点移到前面。使用数组存放结点值,位置移动后再转为链表
class Solution(object):
def rotateRight(self, head, k):
if not head:
return
res = []
while head:
res.append(head.val)
head = head.next
K = k%len(res)
res = res[-K:] + res[:-K]
head = h = ListNode(None)
for i in res:
head.next = ListNode(i)
head = head.next
return h.next
# 方法二:将链表首尾连接成环,再根据要求确定新的头结点,断开环
class Solution(object):
def rotateRight(self, head, k):
if not head or not head.next:
return head
h = head
length = 1
while h.next:
length += 1
h = h.next
h.next = head
step = length - k%length
for _ in range(step):
h = h.next
res = h.next
h.next = None
return res