Skip to content

Commit f3c7bfb

Browse files
authored
Create 1171. Remove Zero Sum Consecutive Nodes from Linked List (#427)
2 parents f40ac67 + 7cc2c1c commit f3c7bfb

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
ListNode* removeZeroSumSublists(ListNode* head) {
4+
ListNode node=ListNode(0, head);
5+
ListNode* ptr=&node;
6+
int prefix=1000000;// The min value for prefix>=-1e6
7+
static ListNode* mp[2000001]={NULL};
8+
for( ; ptr; ptr=ptr->next){
9+
prefix+=(ptr->val);
10+
mp[prefix]=ptr;
11+
}
12+
prefix=1000000, ptr=&node;//reset
13+
for( ;ptr; ptr=ptr->next){
14+
prefix+=(ptr->val);
15+
ptr->next=mp[prefix]->next;
16+
}
17+
return node.next;
18+
}
19+
};

0 commit comments

Comments
 (0)