Skip to content

Practice Merge Nodes in Between Zeros

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2181
LeetCode Problem

Merge Nodes in Between Zeros

Problem Statement

You are given the head of a linked list, which contains a series of integers separated by 0's. The beginning and end of the linked list will have Node.val == 0. For every two consecutive 0's, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0's. Return the head of the modified linked list.

Source: Merge Nodes in Between Zeros on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
head = [0,3,1,0,4,5,2,0]
Output
[4,11]

# Example 2

Input
head = [0,1,0,3,0,2,2,0]
Output
[1,3,4]

Constraints

The number of nodes in the list is in the range [3, 2 * 10^5].
0 <= Node.val <= 1000
There are no two consecutive nodes with Node.val == 0.
The beginning and end of the linked list have Node.val == 0.

Related LeetCode Problems

Practice more problems similar to Merge Nodes in Between Zeros.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.