1
# Runtime: 59 ms (Top 77.27%) | Memory: 14.3 MB (Top 57.10%)
2

3

4
# Definition for singly-linked list.
5
# class ListNode:
6
# def __init__(self, val=0, next=None):
7
# self.val = val
8
# self.next = next
9
class Solution:
10
def removeZeroSumSublists(self, head: Optional[ListNode]) -> Optional[ListNode]:
11
root = ListNode(0, head)
12
summ, d, node = 0, {}, root
13
while node:
14
summ += node.val
15
if summ in d:
16
prev = d[summ]
17
tmp = prev.next
18
tmp_sum = summ
19
while tmp != node:
20
tmp_sum += tmp.val
21
if tmp_sum in d and d[tmp_sum] == tmp:
22
d.pop(tmp_sum)
23
tmp = tmp.next
24
prev.next = node.next
25
node = prev
26
else:
27
d[summ] = node
28
node = node.next
29

30
return root.next

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0