1171.cpp (695B)
1 class Solution { 2 public: 3 ListNode *removeZeroSumSublists(ListNode *head) const { 4 unordered_map<int, ListNode *> um; 5 ListNode dummy(0, head); 6 int crnt = 0; 7 8 for (ListNode *c = &dummy; c; c = c->next) { 9 crnt += c->val; 10 if (um.count(crnt)) { 11 ListNode *p = um[crnt]; 12 c = p->next; 13 14 int v = crnt + c->val; 15 while (v != crnt) { 16 um.erase(v); 17 c = c->next; 18 v += c->val; 19 } 20 21 p->next = c->next; 22 } else 23 um[crnt] = c; 24 } 25 26 return dummy.next; 27 } 28 };