The Indian Engineer

Problem 2181 Merge Nodes in Between Zeros

Posted on Last edited 3 mins

Linked-List Pointers

Problem Statement

Link - Problem 2181

Question

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.

Example 1

graph LR A((0))-->B((3)) B-->C((1)) C-->D((0)) D-->E((4)) E-->F((5)) F-->G((2)) G-->H((0))
Input: head = [0,3,1,0,4,5,2,0]
Output: [4,11]
Explanation:
The above figure represents the given linked list. The modified list contains
- The sum of the nodes marked in green: 3 + 1 = 4.
- The sum of the nodes marked in red: 4 + 5 + 2 = 11.

Example 2

graph LR A((0))-->B((1)) B-->C((0)) C-->D((3)) D-->E((0)) E-->F((2)) F-->G((2)) G-->H((0))
Input: head = [0,1,0,3,0,2,2,0]
Output: [1,3,4]
Explanation:
The above figure represents the given linked list. The modified list contains
- The sum of the nodes marked in green: 1 = 1.
- The sum of the nodes marked in red: 3 = 3.
- The sum of the nodes marked in yellow: 2 + 2 = 4.

Constraints

Solution

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeNodes(ListNode* head) {
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ListNode* dummy = new ListNode(0);
    ListNode* prev = dummy;
    int sum;
    ListNode* curr = head;
    while (curr != nullptr) {
        sum = 0;
        while (curr != nullptr && curr->val == 0) {
            curr = curr->next;
        }

        while (curr != nullptr && curr->val != 0) {
            sum += curr->val;
            curr = curr->next;
        }

        if (sum != 0) {
            prev->next = new ListNode(sum);
            prev = prev->next;
        }
    }

    return dummy->next;
}

};

Complexity Analysis

Explanation

1. Intuition

2. Implementation