-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path025.K个一组翻转链表.py
57 lines (54 loc) · 1.56 KB
/
025.K个一组翻转链表.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
# 方法一
class Solution(object):
def reverseKGroup(self, head, k):
h = ListNode(-1)
h.next = head
pre = h
cur = head
while cur:
t = cur
count = 1
# 遍历k个为一组
while count<k and t:
t = t.next
count += 1
# 凑够一组进行翻转
if count==k and t:
# 每次将当前结点的后一个结点移到该组的最前面
for _ in range(k-1):
lat = cur.next
cur.next = lat.next
lat.next = pre.next
pre.next = lat
pre = cur
cur = cur.next
# 凑不够一组则结束
else:
break
return h.next
# 方法二:递归
class Solution(object):
def reverseKGroup(self, head, k):
h = ListNode(-1)
h.next = head
pre = h
cur = head
t = 1
while cur:
if t % k == 0:
pre = self._reverseGroup(pre, cur.next)
cur = pre.next
else:
cur = cur.next
t += 1
return h.next
def _reverseGroup(self, pre, lat):
lpre = pre.next
cur = lpre.next
# 每次将cur指向的结点移到该组最前面,然后cur指针再指向下一个结点
while cur != lat:
lpre.next = cur.next
cur.next = pre.next
pre.next = cur
cur = lpre.next
return lpre