|
| 1 | +# [2181. Merge Nodes in Between Zeros (Medium)](https://leetcode.com/problems/merge-nodes-in-between-zeros/) |
| 2 | + |
| 3 | +<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> |
| 4 | + |
| 5 | +<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> |
| 6 | + |
| 7 | +<p>Return <em>the</em> <code>head</code> <em>of the modified linked list</em>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong>Example 1:</strong></p> |
| 11 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/02/02/ex1-1.png" style="width: 600px; height: 41px;"> |
| 12 | +<pre><strong>Input:</strong> head = [0,3,1,0,4,5,2,0] |
| 13 | +<strong>Output:</strong> [4,11] |
| 14 | +<strong>Explanation:</strong> |
| 15 | +The above figure represents the given linked list. The modified list contains |
| 16 | +- The sum of the nodes marked in green: 3 + 1 = 4. |
| 17 | +- The sum of the nodes marked in red: 4 + 5 + 2 = 11. |
| 18 | +</pre> |
| 19 | + |
| 20 | +<p><strong>Example 2:</strong></p> |
| 21 | +<img alt="" src="https://assets.leetcode.com/uploads/2022/02/02/ex2-1.png" style="width: 600px; height: 41px;"> |
| 22 | +<pre><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> |
| 40 | + |
| 41 | + |
| 42 | +**Similar Questions**: |
| 43 | +* [Linked List Components (Medium)](https://leetcode.com/problems/linked-list-components/) |
| 44 | + |
| 45 | +## Solution 1. |
| 46 | + |
| 47 | +```cpp |
| 48 | +// OJ: https://leetcode.com/problems/merge-nodes-in-between-zeros/ |
| 49 | +// Author: github.com/lzl124631x |
| 50 | +// Time: O(N) |
| 51 | +// Space: O(1) |
| 52 | +class Solution { |
| 53 | +public: |
| 54 | + ListNode* mergeNodes(ListNode* head) { |
| 55 | + ListNode dummy, *tail = &dummy; |
| 56 | + while (head) { |
| 57 | + if (head->val == 0) head = head->next; // skip leading `0` |
| 58 | + if (!head) break; |
| 59 | + int sum = 0; |
| 60 | + while (head->val != 0) { // sum numbers before the next `0` |
| 61 | + sum += head->val; |
| 62 | + head = head->next; |
| 63 | + } |
| 64 | + tail->next = new ListNode(sum); // append `sum` |
| 65 | + tail = tail->next; |
| 66 | + } |
| 67 | + return dummy.next; |
| 68 | + } |
| 69 | +}; |
| 70 | +``` |
0 commit comments