<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: ListNode) -> ListNode:
        # Dummy node to handle cases where the head of the list needs to be removed
        dummy = ListNode(0)
        dummy.next = head
        
        # Dictionary to store the cumulative sum and the corresponding node
        cumulative_sum = {0: dummy}
        sum = 0
        
        # Traverse the linked list
        while head:
            sum += head.val
            
            # If the cumulative sum is seen before, remove the nodes between the previous occurrence and the current node
            if sum in cumulative_sum:
                prev_node = cumulative_sum[sum]
                temp = prev_node.next
                temp_sum = sum
                
                # Remove nodes between the previous occurrence and the current node
                while temp != head:
                    temp_sum += temp.val
                    del cumulative_sum[temp_sum]
                    temp = temp.next
                
                # Update the next pointer of the previous node to the node after the current node
                prev_node.next = head.next
            else:
                cumulative_sum[sum] = head
            
            # Move to the next node
            head = head.next
        
        return dummy.next

# Helper function to create a linked list from a list
def create_linked_list(arr):
    if not arr:
        return None
    head = ListNode(arr[0])
    current = head
    for val in arr[1:]:
        current.next = ListNode(val)
        current = current.next
    return head

# Helper function to print a linked list
def print_linked_list(head):
    current = head
    while current:
        print(current.val, end=" ")
        current = current.next
    print()

# Example 1
head = create_linked_list([1, 2, -3, 3, 1])
solution = Solution()
head = solution.removeZeroSumSublists(head)
print_linked_list(head)  # Output: [3, 1] or [1, 2, 1]

# Example 2
head = create_linked_list([1, 2, 3, -3, 4])
solution = Solution()
head = solution.removeZeroSumSublists(head)
print_linked_list(head)  # Output: [1, 2, 4]

# Example 3
head = create_linked_list([1, 2, 3, -3, -2])
solution = Solution()
head = solution.removeZeroSumSublists(head)
print_linked_list(head)  # Output: [1]
29