-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem1171.cpp
35 lines (34 loc) · 1.06 KB
/
problem1171.cpp
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
// first put front node with val = 0 and next -> head
ListNode* front = new ListNode(0, head);
ListNode* start = front;
// range will delete it if sum = 0
// range is [start, end]
// first loop begin of range
while(start != nullptr){
ListNode* end = start->next;
int sum = 0;
// second loop end of range
while(end != nullptr){
sum += end->val;
if(!sum)
start->next = end->next;
end = end->next;
}
start = start->next;
}
return front->next;
}
};