linked list
data structures
algorithm
programming
zero sum removal

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.

Practice algorithms

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:

  1. Calculate Prefix Sums: Traverse the list while computing cumulative prefix sums.
  2. Use a `Hash` Map: Store the first occurrence index of each prefix sum in a hash map.
  3. 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.
  4. Remove Zero-sum Nodes: Adjust the `next` pointers to exclude the zero-sum sublist and continue the process.

Detailed Steps

  1. Initialize Structures:
    • Use a hash map to store prefix sums and their corresponding nodes.
    • Create a dummy node to handle edge cases gracefully.
  2. 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.
  3. Complexity
    • Time Complexity: O(n)O(n), where nn is the number of nodes in the linked list. We traverse the list once.
    • Space Complexity: O(n)O(n) 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.