<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]:
        cur = dummy = ListNode(0)
        dummy.next = head
        prefix = 0

        seen = collections.OrderedDict()

        while cur:
            prefix += cur.val

            if prefix not in seen:
                seen[prefix] = cur
            else:
                node = seen[prefix]
                node.next = cur.next

                while list(seen.keys())[-1] != prefix:
                    seen.popitem()
            
            cur = cur.next
        
        return dummy.next

        # arr = []

        # while head:
        #     arr.append(head.val)
        #     head= head.next

        # start_end = []
        # for i in range(len(arr)):
        #     count = 0
        #     for j in range(i, len(arr)):
        #         count += arr[j]
        #         if count == 0:
        #             start_end.append((i,j))
        # print(start_end)
        # # Merge intervals    
        # start_end = sorted(start_end, key = lambda x: x[0])

        # i = 0
        # while i < len(start_end) - 1:
        #     if start_end[i][1] >= start_end[i+1][0]:
        #         start_end.pop(i+1)
        #     else:
        #         i += 1
        # print(start_end)
        # # Replace 0 intervals with -1001
        # for s, e in start_end:
        #     for i in range(s,e+1):
        #         arr[i] = -1001
        # print(arr)
        # # Fill in dummy
        # dummy = cur = ListNode(0)

        # for v in arr:
        #     if v == -1001:
        #         continue
        #     else:
        #         cur.next = ListNode(v)
        #         cur = cur.next

        # return dummy.next
        

69