<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>

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def removeZeroSumSublists(self, head: ListNode) -> ListNode:
        dummy = ListNode(0)  # Dummy node to handle edge cases
        dummy.next = head
        
        prefix_sum = 0
        prefix_map = {}  # Maps prefix sum to the corresponding node
        current = dummy
        
        # First pass: record the latest occurrence of each prefix sum
        while current:
            prefix_sum += current.val
            prefix_map[prefix_sum] = current
            current = current.next
        
        # Second pass: reset the next pointers to remove zero-sum sublists
        current = dummy
        prefix_sum = 0
        while current:
            prefix_sum += current.val
            # Skip all nodes in between the first and last occurrence of the same prefix sum
            current.next = prefix_map[prefix_sum].next
            current = current.next
        
        return dummy.next

# Helper function to create a linked list from a list of values
def create_linked_list(values):
    dummy = ListNode()
    current = dummy
    for value in values:
        current.next = ListNode(value)
        current = current.next
    return dummy.next

# Helper function to print linked list
def print_linked_list(head):
    values = []
    current = head
    while current:
        values.append(current.val)
        current = current.next
    print(" -> ".join(map(str, values)))

# Test cases
if __name__ == "__main__":
    sol = Solution()

    # Test case 1
    head1 = create_linked_list([1, 2, -3, 3, 1])
    result1 = sol.removeZeroSumSublists(head1)
    print_linked_list(result1)  # Expected output: 3 -> 1

    # Test case 2
    head2 = create_linked_list([1, 2, 3, -3, 4])
    result2 = sol.removeZeroSumSublists(head2)
    print_linked_list(result2)  # Expected output: 1 -> 2 -> 4

    # Test case 3
    head3 = create_linked_list([1, 2, 3, -3, -2])
    result3 = sol.removeZeroSumSublists(head3)
    print_linked_list(result3)  # Expected output: 1

31