-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_0025.py
58 lines (44 loc) · 1.38 KB
/
leetcode_0025.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
# Todo: cleaner solution
from typing import Optional
# Definition for singly-linked list.
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:
return head
# add all nodes to stack list
current = head
stack: list[ListNode] = []
while current:
stack.append(current)
current = current.next
# remove (length % k) from tail
cut_index = len(stack) % k
if cut_index != 0:
next_group = stack[-cut_index] # None or length % k items from right side
stack = stack[:-cut_index]
else:
next_group = None
# now reverse each section
while stack:
group_head = stack[-1]
for _ in range(k - 1):
node = stack.pop()
node.next = stack[-1]
node = stack.pop()
node.next = next_group
if stack:
next_group = group_head
else:
head = group_head
return head
s = Solution()
head = ListNode(1)
current = head
for num in [2,3,4,5]:
current.next = ListNode(num)
current = current.next
reverse_head = s.reverseKGroup(head, 5)