<?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>dummy head Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/dummy-head/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/dummy-head/</link>
	<description></description>
	<lastBuildDate>Sat, 28 Nov 2020 19:08:10 +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>dummy head Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/dummy-head/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1669. Merge In Between Linked Lists</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-1669-merge-in-between-linked-lists/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-1669-merge-in-between-linked-lists/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 28 Nov 2020 19:07:25 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[dummy head]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prev]]></category>
		<category><![CDATA[tail]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7722</guid>

					<description><![CDATA[<p>You are given two linked lists:&#160;list1&#160;and&#160;list2&#160;of sizes&#160;n&#160;and&#160;m&#160;respectively. Remove&#160;list1&#8216;s nodes from the&#160;ath&#160;node to the&#160;bth&#160;node, and put&#160;list2&#160;in their place. The blue edges and nodes in the following&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-1669-merge-in-between-linked-lists/">花花酱 LeetCode 1669. Merge In Between Linked Lists</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 two linked lists:&nbsp;<code>list1</code>&nbsp;and&nbsp;<code>list2</code>&nbsp;of sizes&nbsp;<code>n</code>&nbsp;and&nbsp;<code>m</code>&nbsp;respectively.</p>



<p>Remove&nbsp;<code>list1</code>&#8216;s nodes from the&nbsp;<code>a<sup>th</sup></code>&nbsp;node to the&nbsp;<code>b<sup>th</sup></code>&nbsp;node, and put&nbsp;<code>list2</code>&nbsp;in their place.</p>



<p>The blue edges and nodes in the following figure incidate the result:</p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/11/05/fig1.png" alt=""/></figure>



<p><em>Build the result list and return its head.</em></p>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/11/05/merge_linked_list_ex1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> list1 = [0,1,2,3,4,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
<strong>Output:</strong> [0,1,2,1000000,1000001,1000002,5]
<strong>Explanation:</strong> We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.
</pre>



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



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2020/11/05/merge_linked_list_ex2.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
<strong>Output:</strong> [0,1,1000000,1000001,1000002,1000003,1000004,6]
<strong>Explanation:</strong> The blue edges and nodes in the above figure indicate the result.
</pre>



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



<ul><li><code>3 &lt;= list1.length &lt;= 10<sup>4</sup></code></li><li><code>1 &lt;= a &lt;= b &lt; list1.length - 1</code></li><li><code>1 &lt;= list2.length &lt;= 10<sup>4</sup></code></li></ul>



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



<p>Find the following nodes:<br>1. previous node to the a-th node: prev_a<br>2. the b-th node: node_b<br>3. tail node of list2: tail2<br><br>prev_a-&gt;next = list2<br>tail2-&gt;next = node_b<br><br>return list1</p>



<p>Time complexity: O(m+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* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
    ListNode dummy(0, list1);
    ListNode* prev_a = &amp;dummy;
    for (int i = 0; i &lt; a; ++i) prev_a = prev_a-&gt;next;
    ListNode* node_b = prev_a-&gt;next;
    for (int i = a; i &lt;= b; ++i) node_b = node_b-&gt;next;
    ListNode* tail2 = list2;
    while (tail2-&gt;next) tail2 = tail2-&gt;next;
    
    prev_a-&gt;next = list2;    
    tail2-&gt;next = node_b;
    return list1;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-1669-merge-in-between-linked-lists/">花花酱 LeetCode 1669. Merge In Between Linked Lists</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-1669-merge-in-between-linked-lists/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 19. Remove Nth Node From End of List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-19-remove-nth-node-from-end-of-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-19-remove-nth-node-from-end-of-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 20 Sep 2018 06:54:23 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[dummy head]]></category>
		<category><![CDATA[fast slow]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[node]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4057</guid>

					<description><![CDATA[<p>Problem Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1-&#62;2-&#62;3-&#62;4-&#62;5, and n =&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-19-remove-nth-node-from-end-of-list/">花花酱 LeetCode 19. Remove Nth Node From End of List</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1><strong>Problem</strong></h1>
<p>Given a linked list, remove the <em>n</em>-th node from the end of list and return its head.</p>
<p><strong>Example:</strong></p>
<pre class="crayon:false">Given linked list: <strong>1-&gt;2-&gt;3-&gt;4-&gt;5</strong>, and <strong><em>n</em> = 2</strong>.

After removing the second node from the end, the linked list becomes <strong>1-&gt;2-&gt;3-&gt;5</strong>.
</pre>
<p><strong>Note:</strong></p>
<p>Given <em>n</em> will always be valid.</p>
<p><strong>Follow up:</strong></p>
<p>Could you do this in one pass?</p>
<h1>Solution 0: Cheating! store the nodes in an array</h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode *removeNthFromEnd(ListNode *head, int n) {
    if (!head) return nullptr;
    vector&lt;ListNode *&gt; nodes;
    ListNode *cur = head;
    while (cur) {
      nodes.push_back(cur);
      cur = cur-&gt;next;
    }
    if (n == nodes.size()) return head-&gt;next;
    ListNode* nodes_to_remove = nodes[nodes.size()-n];
    ListNode* parent = nodes[nodes.size() - n - 1];
    parent-&gt;next = nodes_to_remove-&gt;next;
    delete nodes_to_remove;
    return head;
  }
};</pre><p></div></div></p>
<h1>Solution 1: Two passes</h1>
<p>Time complexity: O(L)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode* removeNthFromEnd(ListNode* head, int n) {
    int l = 0;
    ListNode* cur = head;
    while (cur) {
      ++l;
      cur = cur-&gt;next;
    }
    if (n == l) {
      ListNode* ans = head-&gt;next;
      delete head;
      return ans;
    }    
    l -= n;
    cur = head;
    while (--l) cur = cur-&gt;next;
    ListNode* node = cur-&gt;next;
    cur-&gt;next = node-&gt;next;
    delete node;
    return head;
  }
};</pre><p></div></div></p>
<h1><strong>Solution 2: Fast/Slow Pointers + Dummy Head / Prev</strong></h1>
<p>Fast pointer moves n steps first, and then slow pointer starts moving.</p>
<p>When fast pointer reaches tail, slow pointer is n-th node from the end.</p>
<p>Time complexity: O(L)</p>
<p>Space complexity: O(1)</p>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode* fast = head;
    for (int i = 0; i &lt; n; ++i) 
      fast = fast-&gt;next;
    ListNode dummy(0);
    dummy.next = head;
    ListNode* prev = &amp;dummy;
    while (fast) {
      fast = fast-&gt;next;
      prev = prev-&gt;next;
    }
    ListNode* node = prev-&gt;next;
    prev-&gt;next = node-&gt;next;
    delete node;
    return dummy.next;
  }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {
  public ListNode removeNthFromEnd(ListNode head, int n) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode fast = head;
    while (n-- &gt; 0) fast = fast.next;
    ListNode prev = dummy;
    while (fast != null) {
      fast = fast.next;
      prev = prev.next;
    }
    prev.next = prev.next.next;
    return dummy.next;
  }
}</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def removeNthFromEnd(self, head, n):
    dummy = ListNode(0)
    dummy.next = head
    fast, prev = head, dummy
    for _ in range(n): 
      fast = fast.next
    while fast:
      fast, prev = fast.next, prev.next
    prev.next = prev.next.next
    return dummy.next</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-19-remove-nth-node-from-end-of-list/">花花酱 LeetCode 19. Remove Nth Node From End of List</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-19-remove-nth-node-from-end-of-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 328. Odd Even Linked List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-328-odd-even-linked-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-328-odd-even-linked-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 13 Apr 2018 05:23:44 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[dummy head]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=2477</guid>

					<description><![CDATA[<p>Problem 题目大意：给你一个链表，把所有奇数位置的节点串在一起，后面跟着所有串在一起的偶数位置的节点。 Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-328-odd-even-linked-list/">花花酱 LeetCode 328. Odd Even Linked List</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description">
<div>
<h1><strong>Problem</strong></h1>
<p>题目大意：给你一个链表，把所有奇数位置的节点串在一起，后面跟着所有串在一起的偶数位置的节点。</p>
<p>Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.</p>
<p>You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.</p>
<p><b>Example:</b><br />
Given <code>1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL</code>,<br />
return <code>1-&gt;3-&gt;5-&gt;2-&gt;4-&gt;NULL</code>.</p>
<p><b>Note:</b><br />
The relative order inside both the even and odd groups should remain as it was in the input.<br />
The first node is considered odd, the second node even and so on &#8230;</p>
<p><b>Credits:</b><br />
Special thanks to <a href="https://leetcode.com/discuss/user/DjangoUnchained">@DjangoUnchained</a> for adding this problem and creating all test cases.</p>
</div>
</div>
<h1>Solution</h1>
<p>Time complexity: O(n)</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 21 ms
class Solution {
public:
  ListNode* oddEvenList(ListNode* head) {
    if (head == nullptr) return head;
    ListNode dummy_odd(0);
    ListNode dummy_even(0);
    ListNode* prev_odd = &amp;dummy_odd;
    ListNode* prev_even = &amp;dummy_even;
    int index = 0;
    while (head) {      
      auto next = head-&gt;next;
      head-&gt;next = nullptr; // important
      if (index++ &amp; 1) {
        prev_even-&gt;next = head;
        prev_even = head;
      } else {
        prev_odd-&gt;next = head;
        prev_odd = head;
      }
      head = next;
    }
    prev_odd-&gt;next = dummy_even.next;
    return dummy_odd.next;
  }
};</pre><p>V2</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 18 ms
class Solution {
public:
  ListNode* oddEvenList(ListNode* head) {
    if (head == nullptr) return head;
    vector&lt;ListNode&gt; heads(2, ListNode(0));    
    vector&lt;ListNode*&gt; prevs{&amp;heads[0], &amp;heads[1]};
    int index = 0;
    while (head) {
      auto next = head-&gt;next;
      head-&gt;next = nullptr; // important      
      prevs[index]-&gt;next = head;
      prevs[index] = head;
      head = next;
      index ^= 1;
    }
    prevs[0]-&gt;next = heads[1].next;
    return heads[0].next;
  }
};</pre><p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 1 ms
class Solution {
  public ListNode oddEvenList(ListNode head) {
    ListNode[] heads = new ListNode[]{new ListNode(0), new ListNode(0)};
    ListNode[] prevs = new ListNode[]{heads[0], heads[1]};
    int index = 0;
    while (head != null) {
      ListNode next = head.next;
      head.next = null;
      prevs[index].next = head;
      prevs[index] = prevs[index].next;
      head = next;
      index ^= 1;
    }
    prevs[0].next = heads[1].next;
    return heads[0].next;
  }
}</pre><p>&nbsp;</p>
<p>Python3</p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 56 ms
"""
class Solution:
  def oddEvenList(self, head):
    heads = [ListNode(0), ListNode(0)]
    prevs = [heads[0], heads[1]]
    index = 0
    while head:
      nxt = head.next
      head.next = None
      prevs[index].next = head
      prevs[index] = prevs[index].next
      head = nxt
      index ^= 1
    prevs[0].next = heads[1].next
    return heads[0].next</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-328-odd-even-linked-list/">花花酱 LeetCode 328. Odd Even Linked List</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-328-odd-even-linked-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
