-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp2_4.py
52 lines (44 loc) · 1.29 KB
/
p2_4.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
from chapter_2.linked_list_utils import llol, Node
# Method: Construct 2 other lists
# Time: O(n)
# Space: O(1)
def part_around(x: int, head: Node):
low_head = None
high_head = None
low_root = None
high_root = None
elem_now = head
while elem_now:
elem_next = elem_now.next
if elem_now.val >= x:
if not high_root:
high_head = elem_now
high_root = elem_now
else:
high_head.next = elem_now
high_head = elem_now
else:
if not low_head:
low_head = elem_now
low_root = elem_now
else:
low_head.next = elem_now
low_head = elem_now
elem_now = elem_next
if low_root:
low_head.next = high_root # Connect the parts
high_head.next = None
return low_root
else:
return high_root # No low elems
if __name__ == "__main__":
exs = [([1, 2, 3, 4, 5, 6, 7], 4),
([10, 4, 7, 3, 1, 200, 40, 50, 3], 4),
([30, 40, 50], 30),
([1, 2, 5, 3, 2, 200], 3)
]
for arr, x in exs:
head = llol(arr)
print(
f"For\t{head} around partition {x},\ngot\t{part_around(x, head)}")
print("-"*50)