-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathq1171.py
More file actions
56 lines (50 loc) · 1.45 KB
/
q1171.py
File metadata and controls
56 lines (50 loc) · 1.45 KB
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
#!/usr/bin/python3
from typing import List
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def removeZeroSumSublists(self, head: ListNode) -> ListNode:
listArray:List[ListNode] = list()
listArray.append(ListNode(0))
while head:
listArray.append(head)
head = head.next
indexDic = {}
sum = 0
index = 0
while index < len(listArray):
sum += listArray[index].val
if sum in indexDic:
left = indexDic[sum] + 1
sum -= listArray[index].val
listArray.pop(index)
for j in reversed(range(left, index)):
del indexDic[sum]
sum -= listArray[j].val
listArray.pop(j)
index = indexDic[sum]
else:
indexDic[sum] = index
index += 1
listArray.pop(0)
for i in range(len(listArray) - 1):
listArray[i].next = listArray[i + 1]
listArray[len(listArray) - 1].next = None
if len(listArray) > 0:
return listArray[0]
return None
nums = [1,2,3,-3,-2]
head = ListNode(nums[0])
p = head
for i in range(1, len(nums)):
node = ListNode(nums[i])
p.next = node
p = p.next
solu = Solution()
p = solu.removeZeroSumSublists(head)
print("last")
while p:
print(p.val)
p = p.next