-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_305.py
65 lines (51 loc) · 1.26 KB
/
problem_305.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
class Node:
def __init__(self, x):
self.val = x
self.cum = 0
self.next = None
def __str__(self):
string = "["
node = self
while node:
string += "{},{} ->".format(node.val, node.cum)
node = node.next
string += "None]"
return string
def get_nodes(values):
next_node = None
for value in values[::-1]:
node = Node(value)
node.next = next_node
next_node = node
return next_node
def get_list(head):
node = head
nodes = list()
while node:
nodes.append(node.val)
node = node.next
return nodes
def add_cum_sum(head):
node = head
cum_sum = 0
while node:
node.cum = node.val + cum_sum
cum_sum = node.cum
node = node.next
def remove_zero_sum(head):
add_cum_sum(head)
dummy_head = Node(None)
dummy_head.next = head
seen_totals = dict()
node = dummy_head
index = 0
while node:
if node.cum in seen_totals:
seen_totals[node.cum].next = node.next
seen_totals[node.cum] = node
index += 1
node = node.next
return dummy_head.next
# Tests
llist = get_nodes([3, 4, -7, 5, -6, 6])
assert get_list(remove_zero_sum(llist)) == [5]