-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_linklist.py
58 lines (54 loc) · 1.48 KB
/
reverse_linklist.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from datastructure.LinkedList import LinkedList, ListNode
def reverse(head:ListNode): # 尾插法
prev = None
while head:
temp = head.next
head.next = prev
prev = head
head = temp
return prev
def reverse2(head, tail):
prev = tail.next
p = head
while prev != tail:
next = p.next
p.next = prev
prev = p
p = next
tail, head
# 反转位置m到n的链表, 只扫描一趟
# 如果用切段 reverse 法, 需扫描两遍, 所以这里用delete, insert法
# 在 left 和 right 之间, 每遍历一个node就move到pre后一位的位置
def reversebetween(head:ListNode, left, right):
dummy = ListNode(-1)
dummy.next = head
pre = dummy
for i in range(left-1):
pre = pre.next
cur = pre.next
for i in range(right-left):
next = cur.next
cur.next = next.next
next.next = pre.next
pre.next = next
return dummy.next
# k个一组反转链表
# 时间复杂度O(n), 空间复杂度O(1)
def reversekgroup(head:ListNode, k):
dummy = ListNode(-1)
dummy.next = head
pre = dummy
while head:
tail = pre
for i in range(k):
tail = tail.next
if not tail:
return dummy.next
next = tail.next
tail.next = None
head, tail = reverse2(head, tail)
pre.next = head
tail.next = next # 切断反转性能更好
pre = tail
head = tail.next
return dummy.next