Skip to content

Commit 39dc232

Browse files
committed
Time: 65 ms (28.26%) | Memory: 51.9 MB (21.74%) - LeetSync
1 parent 8937df4 commit 39dc232

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var removeZeroSumSublists = function(head) {
13+
const dummy = {};
14+
dummy.next = head;
15+
const hm = new Map();
16+
let pSum = 0;
17+
hm.set(pSum, dummy);
18+
19+
while(head){
20+
pSum += head.val;
21+
if(hm.has(pSum)){
22+
let remove = hm.get(pSum).next, sum = pSum;
23+
while(remove !== head){
24+
sum += remove.val;
25+
hm.delete(sum);
26+
remove = remove.next;
27+
}
28+
hm.get(pSum).next = head.next;
29+
}else hm.set(pSum, head);
30+
head = head.next;
31+
}
32+
return dummy.next;
33+
};

0 commit comments

Comments
 (0)