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

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

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

<p><strong class="example">Example 1:</strong></p>

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

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> head = [1,2,3,-3,4]
<strong>Output:</strong> [1,2,4]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> head = [1,2,3,-3,-2]
<strong>Output:</strong> [1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The given linked list will contain between <code>1</code> and <code>1000</code> nodes.</li>
	<li>Each node in the linked list has <code>-1000 &lt;= node.val &lt;= 1000</code>.</li>
</ul>

# 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]:
        # O(N^2) solution
        # list_head = ListNode()
        # list_head.next = head

        # left = list_head
        # while left is not None:
        #     running_sum = 0
        #     right = left.next
        #     while right is not None:
        #         running_sum += right.val
        #         right = right.next
        #         if running_sum == 0:
        #             left.next = right
        #     left = left.next
            
        # return list_head.next

        # O(N) time, O(N) space
        list_head = ListNode()
        list_head.next = head

        running_sum_to_node_dict = {0: list_head}

        current = head
        running_sum = 0
        while current:
            print(current.val, running_sum, running_sum_to_node_dict.keys())
            running_sum += current.val
            if running_sum in running_sum_to_node_dict:
                # the nodes in between sum to 0
                previous = running_sum_to_node_dict[running_sum]

                # running_sum was same at the previous node
                # start adding values from the next nodes and deleting
                #   those sums from dict until the current node (which is not in dict)
                next_node = previous.next
                sum_to_delete = running_sum
                while next_node != current:
                    sum_to_delete +=  next_node.val
                    del running_sum_to_node_dict[sum_to_delete]
                    next_node = next_node.next
                # connect the previous node to the next node (deleting the connection to current)   
                previous.next = current.next
            else:
                running_sum_to_node_dict[running_sum] = current
            current = current.next
            
        return list_head.next
        
        

119