<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>skip Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/skip/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/skip/</link>
	<description></description>
	<lastBuildDate>Wed, 02 Mar 2022 14:52:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>skip Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/skip/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2181. Merge Nodes in Between Zeros</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-2181-merge-nodes-in-between-zeros/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-2181-merge-nodes-in-between-zeros/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 02 Mar 2022 14:51:51 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[merge]]></category>
		<category><![CDATA[skip]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9522</guid>

					<description><![CDATA[<p>You are given the&#160;head&#160;of a linked list, which contains a series of integers&#160;separated&#160;by&#160;0&#8216;s. The&#160;beginning&#160;and&#160;end&#160;of the linked list will have&#160;Node.val == 0. For&#160;every&#160;two consecutive&#160;0&#8216;s,&#160;merge&#160;all the nodes&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-2181-merge-nodes-in-between-zeros/">花花酱 LeetCode 2181. Merge Nodes in Between Zeros</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>You are given the&nbsp;<code>head</code>&nbsp;of a linked list, which contains a series of integers&nbsp;<strong>separated</strong>&nbsp;by&nbsp;<code>0</code>&#8216;s. The&nbsp;<strong>beginning</strong>&nbsp;and&nbsp;<strong>end</strong>&nbsp;of the linked list will have&nbsp;<code>Node.val == 0</code>.</p>



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



<p>Return&nbsp;<em>the</em>&nbsp;<code>head</code>&nbsp;<em>of the modified linked list</em>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/02/02/ex1-1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [0,3,1,0,4,5,2,0]
<strong>Output:</strong> [4,11]
<strong>Explanation:</strong> 
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.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/02/02/ex2-1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> head = [0,1,0,3,0,2,2,0]
<strong>Output:</strong> [1,3,4]
<strong>Explanation:</strong> 
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.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li>The number of nodes in the list is in the range&nbsp;<code>[3, 2 * 10<sup>5</sup>]</code>.</li><li><code>0 &lt;= Node.val &lt;= 1000</code></li><li>There are&nbsp;<strong>no</strong>&nbsp;two consecutive nodes with&nbsp;<code>Node.val == 0</code>.</li><li>The&nbsp;<strong>beginning</strong>&nbsp;and&nbsp;<strong>end</strong>&nbsp;of the linked list have&nbsp;<code>Node.val == 0</code>.</li></ul>



<h2><strong>Solution: List</strong></h2>



<p>Skip the first zero, replace every zero node with the sum of values of its previous nodes.</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode* mergeNodes(ListNode* head) {
    ListNode dummy;    
    head = head-&gt;next;
    for (ListNode* prev = &amp;dummy; head; head = head-&gt;next) {
      int sum = 0;
      while (head-&gt;val != 0) {
        sum += head-&gt;val;
        head = head-&gt;next;
      }
      prev-&gt;next = head;
      head-&gt;val = sum;
      prev = head;      
    }
    return dummy.next;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-2181-merge-nodes-in-between-zeros/">花花酱 LeetCode 2181. Merge Nodes in Between Zeros</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/list/leetcode-2181-merge-nodes-in-between-zeros/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
