-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp2_3.py
36 lines (28 loc) · 916 Bytes
/
p2_3.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
from chapter_2.linked_list_utils import llol, Node
# Method: Identity theft
# Time: O(1)
# Space: O(1)
def remove_mid(mid_node: Node):
victim = mid_node.next
if not victim:
raise Exception("No following nodes")
# Take the value, and point to the next (can be none)
mid_node.val = victim.val
mid_node.next = victim.next
victim.next = None
if __name__ == "__main__":
exs = [([1, 2, 3, 4, 5], 3),
([1, 1, 1, 1], 1),
([1, 2, 4, 5, 6, 7], 2),
([1, 2, 3], 3)]
for arr, elem in exs:
head = llol(arr)
in_node = head
while in_node.val != elem:
in_node = in_node.next
try:
remove_mid(in_node)
print(f"For input {arr} to remove {elem} got result {head}")
except Exception as e:
print(f"For input {arr} to remove {elem} got error: {e}")
print("-"*50)