|
| 1 | +<h2><a href="https://leetcode.com/problems/merge-nodes-in-between-zeros">2181. Merge Nodes in Between Zeros</a></h2><h3>Medium</h3><hr><p>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>.</p> |
| 2 | + |
| 3 | +<p>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.</p> |
| 4 | + |
| 5 | +<p>Return <em>the</em> <code>head</code> <em>of the modified linked list</em>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/02/02/ex1-1.png" style="width: 600px; height: 41px;" /> |
| 10 | +<pre> |
| 11 | +<strong>Input:</strong> head = [0,3,1,0,4,5,2,0] |
| 12 | +<strong>Output:</strong> [4,11] |
| 13 | +<strong>Explanation:</strong> |
| 14 | +The above figure represents the given linked list. The modified list contains |
| 15 | +- The sum of the nodes marked in green: 3 + 1 = 4. |
| 16 | +- The sum of the nodes marked in red: 4 + 5 + 2 = 11. |
| 17 | +</pre> |
| 18 | + |
| 19 | +<p><strong class="example">Example 2:</strong></p> |
| 20 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/02/02/ex2-1.png" style="width: 600px; height: 41px;" /> |
| 21 | +<pre> |
| 22 | +<strong>Input:</strong> head = [0,1,0,3,0,2,2,0] |
| 23 | +<strong>Output:</strong> [1,3,4] |
| 24 | +<strong>Explanation:</strong> |
| 25 | +The above figure represents the given linked list. The modified list contains |
| 26 | +- The sum of the nodes marked in green: 1 = 1. |
| 27 | +- The sum of the nodes marked in red: 3 = 3. |
| 28 | +- The sum of the nodes marked in yellow: 2 + 2 = 4. |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong>Constraints:</strong></p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li>The number of nodes in the list is in the range <code>[3, 2 * 10<sup>5</sup>]</code>.</li> |
| 36 | + <li><code>0 <= Node.val <= 1000</code></li> |
| 37 | + <li>There are <strong>no</strong> two consecutive nodes with <code>Node.val == 0</code>.</li> |
| 38 | + <li>The <strong>beginning</strong> and <strong>end</strong> of the linked list have <code>Node.val == 0</code>.</li> |
| 39 | +</ul> |
0 commit comments