-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_nodes_in_k_group.py
39 lines (36 loc) · 1.24 KB
/
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
from typing import Optional
from problems.util.list_node import ListNode
class ReverseNodesInKGroup:
@staticmethod
def reverseKGroup(head: Optional[ListNode], k: int) -> Optional[ListNode]:
# Special case
if head is None or k <= 0:
return head
# Dummy head
dummy = ListNode()
dummy.next = head
# Pointer to traverse the list
temp = dummy
# Traverse the list
while True:
current_temp = temp
# Check if we have k nodes to reverse
index = 0
while index < k and current_temp is not None:
current_temp = current_temp.next
index += 1
if current_temp is None:
break
# Reverse the list
previous_node, current_node, next_node = None, temp.next, None
for i in range(k):
next_node = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = next_node
# Tail of the group
tail = temp.next
tail.next = current_node
temp.next = previous_node
temp = tail
return dummy.next