-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25-ReverseKGroup.py
More file actions
67 lines (56 loc) · 1.97 KB
/
25-ReverseKGroup.py
File metadata and controls
67 lines (56 loc) · 1.97 KB
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
59
60
61
62
63
64
65
66
67
# Python3
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# 递归
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
if not head or not head.next or k == 1:
return head
ret = head
for _ in range(k-1): # ret遍历至第k个节点,用于后续返回头节点
if not ret.next:
return head
else:
ret = ret.next
cur = head.next
pre = head
for _ in range(k-1): # 修改k-1个节点的next指针,最终pre为第k个节点,cur为第k+1个节点
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
head.next = self.reverseKGroup(cur, k)
head = ret # 返回头节点
return head
# 迭代
# 时间复杂度:O(n),其中 n 为链表的长度。空间复杂度:O(1),只需要建立常数个变量。
class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
hair = ListNode(0, head)
pre = hair
while head:
tail = pre
# 查看剩余部分长度是否大于等于k
for i in range(k):
tail = tail.next
if not tail:
return hair.next
nextTmp = tail.next
head, tail = self.reverse(head, tail)
# 把子链表重新拼接
pre.next = head
tail.next = nextTmp
pre = tail # 新的一轮分组
head = tail.next
# 翻转一个子链表,并且返回新的头与尾
def reverse(self, head: ListNode, tail: ListNode):
pre = tail.next # 初始值设置为tail.next,用于赋值给head.next
cur = head
while pre != tail:
nextTmp = cur.next
cur.next = pre
pre = cur
cur = nextTmp
return tail, head