forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1171.cpp
More file actions
29 lines (29 loc) · 739 Bytes
/
Copy path1171.cpp
File metadata and controls
29 lines (29 loc) · 739 Bytes
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
class Solution
{
public:
ListNode* removeZeroSumSublists(ListNode* head)
{
ListNode* du = new ListNode(0), *cur = du;
du->next = head;
int preSum = 0;
map<int, ListNode*> m;
while (cur)
{
preSum += cur->val;
if (m.count(preSum))
{
cur = m[preSum]->next;
int p = preSum + cur->val;
while (p != preSum)
{
m.erase(p);
cur = cur->next;
p += cur->val;
}
m[preSum]->next = cur->next;
} else m[preSum] = cur;
cur = cur->next;
}
return du->next;
}
};