Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.

After doing so, return the head of the final linked list.&nbsp; You may return any such answer.

&nbsp;
(Note that in the examples below, all sequences are serializations of ListNode objects.)

Example 1:

Input: head = [1,2,-3,3,1]
Output: [3,1]
Note: The answer [1,2,1] would also be accepted.


Example 2:

Input: head = [1,2,3,-3,4]
Output: [1,2,4]


Example 3:

Input: head = [1,2,3,-3,-2]
Output: [1]


&nbsp;
Constraints:


	The given linked list will contain between 1 and 1000 nodes.
	Each node in the linked list has -1000 &lt;= node.val &lt;= 1000.


# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
    def removeZeroSumSublists(self, head: Optional[ListNode]) -> Optional[ListNode]:
        root = ListNode(0,head)
        summ , d , node = 0 , {} , root
        while node:
            summ += node.val
            if summ in d:
                prev = d[summ]
                tmp = prev.next
                tmp_sum = summ
                while tmp != node:
                    tmp_sum += tmp.val
                    if tmp_sum in d and d[tmp_sum] == tmp :d.pop(tmp_sum)
                    tmp = tmp.next
                prev.next = node.next
                node = prev
            else:
                d[summ] = node
            node = node.next

        return root.next
class Solution {
    public ListNode removeZeroSumSublists(ListNode head) {
        
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        int prefix = 0;
        ListNode curr = dummy;
        Map<Integer, ListNode> seen = new HashMap<>();
        seen.put(prefix, dummy);
        
        while (curr != null) {
            prefix += curr.val;
            seen.put(prefix, curr);
            curr = curr.next;
        }
        
        prefix = 0;
        curr = dummy;
        while (curr != null) {
            prefix += curr.val;
            curr.next = seen.get(prefix).next;
            curr = curr.next;
        }
        
        return dummy.next;
    }
}
class Solution {
public:
    ListNode* removeZeroSumSublists(ListNode* head) {
        unordered_map<int, ListNode*> m; // {prefix -> node}
        ListNode* dummy = new ListNode(0);
        ListNode* cur = dummy;
        dummy->next = head;
        int prefix = 0;
        while(cur){
            prefix += cur->val;
            if(m[prefix] != NULL){
                ListNode *t = m[prefix]->next;
                int sum = prefix;
                sum+=t->val;
                while(sum != prefix){
                    m.erase(sum);
                    t = t->next;
                    sum+= t->val;
                }
                m[prefix]->next = cur->next;
            }
            else m[prefix] = cur;
            cur = cur->next;
        }
        return dummy->next;
    }
};
var removeZeroSumSublists = function(head) {
    const dummyHead = new ListNode();
    dummyHead.next = head;
    let prev = dummyHead;
    let start = head;
    
    while (start != null) {
        let sum = 0;
        let tail = start;
        
        while (tail != null) {
            sum += tail.val;
            if (sum === 0) break;
            tail = tail.next;
        }
        
        if (tail) {
            prev.next = tail.next;
            start = tail.next;
        }
        else {
            prev = start;
            start = start.next;
        }
    }
    
    
    return dummyHead.next;
};
Remove Zero Sum Consecutive Nodes from Linked List