-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path25.reverse_nodes_in_k-group.py
75 lines (60 loc) · 1.78 KB
/
25.reverse_nodes_in_k-group.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import List,Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if k <= 1 or not head:
return head
#把lastTail后面的s个点逆序, 返回逆序部分的最后一个
def reverse(lastTail: Optional[ListNode], curK: int) -> Optional[ListNode]:
tail = lastTail.next
if curK < 2 or not tail:
return tail
head = lastTail
c = 1
n = tail.next
while n:
tail.next = n.next
n.next = head.next
head.next = n
n = tail.next
c += 1
if c == curK:
return tail
if c < curK:
return reverse(lastTail, c)
return tail
extraHead = ListNode(None, head)
lastTail = extraHead
while lastTail.next:
lastTail = reverse(lastTail, k)
return extraHead.next
if __name__ == "__main__":
def makeList(l: List):
if l == None or len(l) == 0:
return None
ret = ListNode(None, None)
tail = ret
for v in l:
tail.next = ListNode(v)
tail = tail.next
return ret.next
def convertList(ln):
n = ln
ret = []
while n is not None:
ret.append(n.val)
n = n.next
return ret
def printList(ln):
print(convertList(ln))
l = [1 ,2]
k = 2
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l = [1, 2, 3, 4, 5]
k = 3
s = Solution()
bl = s.reverseKGroup(makeList(l), k)
printList(bl)