-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsolution.cpp
77 lines (72 loc) · 2.06 KB
/
solution.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <unordered_map>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode(int x): val(x), next(NULL) {}
};
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
int curSum = 0;
ListNode* cur = head;
unordered_map<int, ListNode*> seen_sum;
seen_sum[0] = cur;
while (cur) {
curSum += cur->val;
ListNode* seen_node = NULL;
if (seen_sum.find(curSum) != seen_sum.end()) {
seen_node = seen_sum[curSum];
}
if (seen_node) {
ListNode* temp;
if (curSum == 0) {
temp = seen_node;
}
else {
temp = seen_node->next;
}
int temp_sum = curSum;
// remove the extra keys from the seen_sum
while (temp != cur) {
temp_sum += temp->val;
seen_sum.erase(temp_sum);
temp = temp->next;
}
}
if (curSum == 0) {
// delete all nodes from first node to cur node
seen_sum[0] = cur->next;
} else if (seen_node) {
// update the next pointer to delete the intermediate nodes
seen_node->next = cur->next;
} else {
seen_sum[curSum] = cur;
}
cur = cur->next;
}
return seen_sum[0];
}
void printList(ListNode* head) {
while (head) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
};
int main () {
ListNode* head = new ListNode(1);
ListNode* cur = head;
head->next = new ListNode(2);
cur = cur->next;
cur->next = new ListNode(3);
cur = cur->next;
cur->next = new ListNode(-3);
cur = cur->next;
cur->next = new ListNode(-2);
Solution solver;
solver.removeZeroSumSublists(head);
solver.printList(head); // 1
}