<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]:        
        if head is None:
            return None
        
        def getList(head):
            res = []
            while head:
                res.append(head.val)
                head=  head.next
            return res
        

        numsList = getList(head)

        prefixHash = {}
        prefixSum = []
        newList = []
        for num in numsList:
            if num == 0:
                continue
            newList.append(num)
            if prefixSum:
                prefixSum.append(prefixSum[-1] + num)
            else:
                prefixSum.append(num)
            
            curSum  = prefixSum[-1]
            
            if curSum in prefixHash: 
                endIndex = prefixHash[curSum]
                prefixSum = prefixSum[:endIndex]
                newList = newList[:endIndex]
                # after a range is deleted I will have to delete all existing indexes of that range
                prevHashes = list(prefixHash.keys())
                for prevHash in prevHashes:
                    if prefixHash[prevHash] >= endIndex:
                        del prefixHash[prevHash]
            else:
                prefixHash[prefixSum[-1] - num] = len(newList) - 1

        head = root = ListNode()
        for num in newList:
            head.next = ListNode(num)
            head = head.next
        
        return root.next
        


        # we have to delete this block and also update the prefixSum by removing sum till that index
        # sum[i:j] = sum[:j] - sum[:i] + nums[i] = 0
        # sum[:j] = sum[:i] - nums[i]

        # # -3 3 =>   []
        # # 3 -3 =>   []
        # # 1 2 -3 => []
        # # 1 -3 2 => []
        # # 4 -3

        # # If I have all the prefix Sums, I can predict if there can be a subArr which will give res as 0
        # # clear 
        # # take a negative number and go left and right from it and check to clear it

        # subArr sum = 0

        # find all subArr that has curIndex element


        # 1 -3 1 -2 3 


        
58