Update appNew update is available. Click here to update.

Sum Between Zeroes

Contributed by
Vishal Modani
Last Updated: 23 Feb, 2023
Easy
yellow-spark
0/40
Avg time to solve 20 mins
Success Rate 80 %
Share
25 upvotes

Problem Statement

Suggest Edit

You are given a Singly Linked List which contains a series of integers separated by ‘0’.

Between two zeroes, you have to merge all the nodes lying between them into a single node which contains the sum of all the merged nodes. You have to perform this in place.

Note:

It is guaranteed that there will be no two consecutive zeroes, and there will always be a zero at the beginning and end of the linked list.
Detailed explanation ( Input/output format, Notes, Images )

Sample Input 1:

0 1 2 3 0 4 5 0 6 0 -1

Sample Output 1:

6 9 6 -1

Explanation Of Sample Input1:

The given linked list is:
0 -> 1 -> 2 -> 3 -> 0 -> 4 -> 5 -> 0 -> 6 -> 0 -> null
Then, the linked list is converted to:
6 -> 9 -> 6 -> null
Taking 0s as the start and end in reference to a sequence, we can see that there are 3 sequences. They are:
 1. 1 -> 2 -> 3, which sum to 6
 2. 4 -> 5, which sum to 9
 3. 6, which sum to 6 only

Sample Input 2:

0 1 2 4 8 16 0 -1

Sample Output 2:

31 -1
Reset Code
Full screen
Auto
copy-code
Console