remove elements from link list whose sum equals to zero
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Linked lists are a fundamental data structure used for efficient data storage and manipulation, particularly when dynamic memory allocation is necessary. While handling linked lists, a common problem arises: removing contiguous sublists whose node values sum to zero. This task becomes crucial in scenarios where such zero-sum sequences are considered redundant or need to be cleaned for further processing.
This article explores methods to tackle this problem with technical explanations and examples.
Problem Statement
Given a singly linked list, our goal is to identify and remove all contiguous sublists whose sum equals zero. This task requires iterating through the list and keeping track of sums efficiently to detect zero-sum sublists.
Approach
To solve the problem, we can utilize a combination of hash maps (or dictionaries in Python) and prefix sums. Here's a step-by-step outline:
- Calculate Prefix Sums: Traverse the list while computing cumulative prefix sums.
- Use a `Hash` Map: Store the first occurrence index of each prefix sum in a hash map.
- Identify Zero-sum Sublists: If a prefix sum repeats, it indicates that the sum of nodes between the first occurrence and the current node is zero.
- Remove Zero-sum Nodes: Adjust the `next` pointers to exclude the zero-sum sublist and continue the process.
Detailed Steps
- Initialize Structures:
- Use a hash map to store prefix sums and their corresponding nodes.
- Create a dummy node to handle edge cases gracefully.
- Traverse and Compute:
- Iterate through the linked list, computing the prefix sum at each node.
- If a prefix sum has not been seen, store it with the current node in the hash map.
- If a prefix sum is seen again, it indicates a zero-sum sublist. Adjust the `next` pointers to skip this sequence.
- Complexity
- Time Complexity: , where is the number of nodes in the linked list. We traverse the list once.
- Space Complexity: because of the hash map used to store prefix sums.
Code Example
Below is a Python implementation using the approach described:
- Start from the first node. Calculate prefix sums.
- Detect zero-sum sequence `[1, 2, -3]` and remove it.
- Resulting list: `3 -> 1 -> -1`.
- On further inspection, remove `[3, 1, -1]` as it sums to zero.
- Final list: Empty.
Related reading
- Remove item from list based on condition
- Remove nodes from graph or reset entire default graph
- Remove redundant parentheses from an arithmetic expression
- Remove substrings inside a list with better than On2 complexity
- Remove empty array elements
- Remove empty elements from an array in Javascript
- Remove the minimum number of blades
- Removing almost duplicate strings in subquadratic time

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.