> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Merge Nodes in Between Zeros Python Solution

> Tested Python solution for LeetCode 2181 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2181, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/merge-nodes-in-between-zeros/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2181   # by problem number
lcpy gen -s merge_nodes_in_between_zeros   # by problem name
```

## Problem

You are given the \<code>head\</code> of a linked list, which contains a series of integers \<strong>separated\</strong> by \<code>0\</code>'s. The \<strong>beginning\</strong> and \<strong>end\</strong> of the linked list will have \<code>Node.val == 0\</code>.

For \<strong>every\</strong> two consecutive \<code>0\</code>'s, \<strong>merge\</strong> all the nodes lying in between them into a single node whose value is the \<strong>sum\</strong> of all the merged nodes. The modified list should not contain any \<code>0\</code>'s.

Return \<em>the head of the modified linked list\</em>.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2022/02/02/ex1-1.png)

```
Input: head = [0,3,1,0,4,5,2,0]
Output: [4,11]
Explanation: The modified list contains the sum of the nodes marked in green: 3 + 1 = 4, and the sum of the nodes marked in red: 4 + 5 + 2 = 11.
```

![Example 2](https://assets.leetcode.com/uploads/2022/02/02/ex2-1.png)

```
Input: head = [0,1,0,3,0,2,2,0]
Output: [1,3,4]
Explanation: The modified list contains the sum of the nodes marked in green: 1 = 1, the sum of the nodes marked in red: 3 = 3, and the sum of the nodes marked in yellow: 2 + 2 = 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`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/merge_nodes_in_between_zeros/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/merge_nodes_in_between_zeros/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n) where n is the number of nodes in the input list
    # Space: O(1), nodes are merged in place
    def merge_nodes(self, head: ListNode[int] | None) -> ListNode[int] | None:
        if head is None:
            return None
        tail = head
        node = head.next
        total = 0
        first = True
        while node is not None:
            if node.val == 0:
                if first:
                    head.val = total
                    first = False
                else:
                    nxt = tail.next
                    assert nxt is not None
                    nxt.val = total
                    tail = nxt
                total = 0
            else:
                total += node.val
            node = node.next
        tail.next = None
        return head
```

## Complexity

| Time | Space |
| - | - |
| O(n) where n is the number of nodes in the input list | O(1), nodes are merged in place |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.