linked list November 28, 2022

Remove zero sum consecutive nodes from linked list

Time O(n) Space O(n) Open original problem

We will keep track of the prefix sum of the linked list. If the prefix sum is repeated, we will remove the nodes between the repeated prefix sum and the current prefix sum.

# 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]:
        dummy = ListNode(0, head)
        prefix = 0

        lookup = {0:dummy}
        while head:
            prefix += head.val
            lookup[prefix] = head
            head = head.next

        head = dummy
        prefix = 0
        while head:
            prefix += head.val
            head.next = lookup[prefix].next
            head = head.next

        return dummy.next

Time complexity: O(n), n is the number of nodes in the linked list
Space complexity: O(n)