Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

1171. Remove Zero Sum Consecutive Nodes from Linked List

Medium52.8% Acceptance
Hash TableLinked List
Asked by:
A
Amazon
ProblemHints (3)Solutions (12)VideosCompanies (1)Notes

Problem Statement

Given the head of a linked list, we repeatedly delete consecutive sequences of nodes that sum to 0 until there are no such sequences.

After doing so, return the head of the final linked list.  You may return any such answer.

(Note that in the examples below, all sequences are serializations of ListNode objects.)

Example 1:

Input: head = [1,2,-3,3,1]
Output: [3,1]
Note: The answer [1,2,1] would also be accepted.

Example 2:

Input: head = [1,2,3,-3,4]
Output: [1,2,4]

Example 3:

Input: head = [1,2,3,-3,-2]
Output: [1]

Constraints:

  • The given linked list will contain between 1 and 1000 nodes.
  • Each node in the linked list has -1000 <= node.val <= 1000.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

The key idea in #1171 Remove Zero Sum Consecutive Nodes from Linked List is recognizing that a sequence of nodes sums to zero when two positions in the list have the same prefix sum. As you traverse the linked list, maintain a running sum and store it in a hash table mapping each prefix sum to the most recent node where it appears.

If the same prefix sum appears again, it means the nodes between those two positions sum to 0. You can remove that entire segment by adjusting pointers to skip those nodes. A common strategy uses a two-pass traversal: the first pass records prefix sums in a map, and the second pass reconnects nodes using the stored references to eliminate zero-sum segments.

This approach efficiently handles overlapping zero-sum ranges and ensures the final list contains only nodes that cannot form a zero-sum consecutive sequence. The solution runs in linear time with additional space for the hash map.

Complexity

ApproachTime ComplexitySpace Complexity
Prefix Sum with Hash Map (Two Pass)O(n)O(n)

Video Solution Available

leetuition

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Convert the linked list into an array.

2
Hint 2

While you can find a non-empty subarray with sum = 0, erase it.

3
Hint 3

Convert the array into a linked list.

Ready to see the solutions?View Solutions

Solutions (12)

Prefix Sum Hash Map

This approach uses a hashmap to record prefix sums while iterating through the linked list. When a prefix sum is repeated, it indicates that the nodes between the occurrences of the prefix sum form a zero-sum sequence.

Time Complexity: O(n) where n is the number of nodes. Each node is visited at most twice.

Space Complexity: O(n) for the hashmap storing prefix sums.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4struct ListNode {
5    int val;
6    struct ListNode 




Explanation

This solution initializes a dummy node to handle edge cases and a hashmap to track prefix sums. As the list is traversed, prefix sums are calculated and stored. When a duplicate prefix sum is found, nodes forming a zero-sum are bypassed using the map.

Iterative Node Skipping

This approach keeps processing the list until no zero-sum sublists can be found. This involves iterative rescanning of the list and skipping nodes accordingly.

Time Complexity: O(n^2) worst case, due to repeated scanning of the list.

Space Complexity: O(1), only a few local variables.

CC++JavaPythonC#JavaScript
1



Video Solutions

Watch expert explanations and walkthroughs

LeetCode 1171 | Remove Zero Sum Consecutive Nodes from Linked List

leetuition
12:3916,699 views

Asked By Companies

1 companies
A
Amazon

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Copy List with Random PointerMedium
Linked List CycleEasy
Linked List Cycle IIMedium
LRU CacheMedium
More similar problems

Related Topics

Hash TableLinked List

Problem Stats

Acceptance Rate52.8%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why are prefix sums useful in this linked list problem?

Prefix sums help track the cumulative sum from the start of the list to the current node. If the same sum appears twice, the nodes between them must sum to zero, making it easy to identify and remove that section.

Is Remove Zero Sum Consecutive Nodes from Linked List asked in FAANG interviews?

Yes, variations of this problem are common in technical interviews at large tech companies. It tests understanding of prefix sums, hash maps, and pointer manipulation in linked lists.

What data structure is best for Remove Zero Sum Consecutive Nodes from Linked List?

A hash map is the most effective data structure for this problem. It stores prefix sums as keys and the latest node where the sum appears, allowing quick detection and removal of zero-sum consecutive segments.

What is the optimal approach for Remove Zero Sum Consecutive Nodes from Linked List?

The optimal approach uses a prefix sum technique combined with a hash map. By storing prefix sums and their corresponding nodes, you can detect when a sublist sums to zero and remove it efficiently in linear time.

*
next
;
7
}
;
8
9
struct
ListNode
*
removeZeroSumSublists
(
struct
ListNode
*
head
)
{
10
struct
ListNode
*
dummy
=
malloc
(
sizeof
(
struct
ListNode
)
)
;
11
dummy
->
val
=
0
;
12
dummy
->
next
=
head
;
13
struct
ListNode
*
cur
=
dummy
;
14
int
prefix_sum
=
0
;
15
16
struct
ListNode
*
*
seen
=
calloc
(
1001
,
sizeof
(
struct
ListNode
*
)
)
;
17
seen
[
0
]
=
dummy
;
18
19
while
(
cur
)
{
20
prefix_sum
+=
cur
->
val
;
21
22
if
(
seen
[
prefix_sum
]
)
{
23
struct
ListNode
*
node
=
seen
[
prefix_sum
]
->
next
;
24
int
p
=
prefix_sum
;
25
while
(
node
!=
cur
)
{
26
p
+=
node
->
val
;
27
seen
[
p
]
=
NULL
;
28
node
=
node
->
next
;
29
}
30
seen
[
prefix_sum
]
->
next
=
cur
->
next
;
31
}
else
{
32
seen
[
prefix_sum
]
=
cur
;
33
}
34
35
cur
=
cur
->
next
;
36
}
37
return
dummy
->
next
;
38
}
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
struct
ListNode
{
5
int
val
;
6
struct
ListNode
*
next
;
7
}
;
8
9
struct
ListNode
*
removeZeroSumSublists
(
struct
ListNode
*
head
)
{
10
struct
ListNode
dummy
,
*
cur
,
*
node
,
*
prev
;
11
int
sum
=
0
,
found
;
12
dummy
.
next
=
head
;
13
14
do
{
15
found
=
0
;
16
cur
=
&
dummy
;
17
sum
=
0
;
18
while
(
cur
->
next
)
{
19
sum
+=
cur
->
next
->
val
;
20
node
=
cur
->
next
;
21
if
(
sum
==
0
)
{
22
cur
->
next
=
node
->
next
;
23
found
=
1
;
24
}
else
{
25
cur
=
cur
->
next
;
26
}
27
}
28
}
while
(
found
)
;
29
30
return
dummy
.
next
;
31
}

Explanation

The C solution repeatedly scans the list, calculates a running sum, and uses a nested loop to remove nodes in zero sum sequences. This continues until no more zero sum sequences are identified.