<?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>node Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/node/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/node/</link>
	<description></description>
	<lastBuildDate>Fri, 30 Oct 2020 03:35:05 +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>node Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/node/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 61. Rotate List</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 29 Oct 2020 05:59:44 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[rotate]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=7573</guid>

					<description><![CDATA[<p>Given a linked&#160;list, rotate the list to the right by&#160;k&#160;places, where&#160;k&#160;is non-negative. Example 1: Input: 1-&#62;2-&#62;3-&#62;4-&#62;5-&#62;NULL, k = 2 Output: 4-&#62;5-&#62;1-&#62;2-&#62;3-&#62;NULL Explanation: rotate 1 steps&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/">花花酱 LeetCode 61. Rotate 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[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 61. Rotate List - 刷题找工作 EP365" width="500" height="281" src="https://www.youtube.com/embed/a4XZu2VVE9Q?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Given a linked&nbsp;list, rotate the list to the right by&nbsp;<em>k</em>&nbsp;places, where&nbsp;<em>k</em>&nbsp;is non-negative.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL, k = 2
<strong>Output:</strong> 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL
<strong>Explanation:</strong>
rotate 1 steps to the right: 5-&gt;1-&gt;2-&gt;3-&gt;4-&gt;NULL
rotate 2 steps to the right: 4-&gt;5-&gt;1-&gt;2-&gt;3-&gt;NULL
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> 0-&gt;1-&gt;2-&gt;NULL, k = 4
<strong>Output:</strong> <code>2-&gt;0-&gt;1-&gt;NULL</code>
<strong>Explanation:</strong>
rotate 1 steps to the right: 2-&gt;0-&gt;1-&gt;NULL
rotate 2 steps to the right: 1-&gt;2-&gt;0-&gt;NULL
rotate 3 steps to the right:&nbsp;<code>0-&gt;1-&gt;2-&gt;NULL</code>
rotate 4 steps to the right:&nbsp;<code>2-&gt;0-&gt;1-&gt;NULL</code></pre>



<h2><strong>Solution: Find the prev of the new head</strong></h2>



<figure class="wp-block-image size-large"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/61-ep365.png" alt="" class="wp-image-7576" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/61-ep365.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/61-ep365-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2020/10/61-ep365-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<p>Step 1: Get the tail node T while counting the length of the list.<br>Step 2: k %= l, k can be greater than l, rotate k % l times has the same effect.<br>Step 3: Find the previous node P of the new head N by moving (l &#8211; k &#8211; 1) steps from head<br>Step 4: set P.next to null, T.next to head and return N</p>



<p>Time complexity: O(n) n is the length of the list<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* rotateRight(ListNode* head, int k) {
    if (!head) return head;    
    int l = 1;
    ListNode* tail = head;
    while (tail-&gt;next) { tail = tail-&gt;next; ++l; }
    k %= l;
    if (k == 0) return head;
    
    ListNode* prev = head;
    while (--l &gt; k) prev = prev-&gt;next;
    ListNode* new_head = prev-&gt;next;
    tail-&gt;next = head;
    prev-&gt;next = nullptr;
    return new_head;
  }
};</pre>

</div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
  public ListNode rotateRight(ListNode head, int k) {
    if (head == null) return null;
    int l = 1;
    ListNode tail = head;
    while (tail.next != null) {
      tail = tail.next;
      ++l;
    }
    k %= l;
    if (k == 0) return head;
    
    ListNode prev = head;
    for (int i = 0; i &lt; l - k - 1; ++i) {
      prev = prev.next;
    }
    
    ListNode new_head = prev.next;
    prev.next = null;
    tail.next = head;
    return new_head;
  }
}</pre>

</div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">class Solution:
  def rotateRight(self, head: ListNode, k: int) -&gt; ListNode:
    if not head: return head
    tail = head
    l = 1
    while tail.next: 
      tail = tail.next
      l += 1
    k = k % l
    if k == 0: return head
    
    prev = head
    for _ in range(l - k - 1): prev = prev.next
    
    new_head = prev.next
    tail.next = head
    prev.next = None
    return new_head</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-61-rotate-list/">花花酱 LeetCode 61. Rotate 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-61-rotate-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 24. Swap Nodes in Pairs</title>
		<link>https://zxi.mytechroad.com/blog/list/leetcode-24-swap-nodes-in-pairs/</link>
					<comments>https://zxi.mytechroad.com/blog/list/leetcode-24-swap-nodes-in-pairs/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 02 Oct 2018 15:59:12 +0000</pubDate>
				<category><![CDATA[List]]></category>
		<category><![CDATA[dummy]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[node]]></category>
		<category><![CDATA[swap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=4122</guid>

					<description><![CDATA[<p>Problem Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1-&#62;2-&#62;3-&#62;4, you should return the list as 2-&#62;1-&#62;4-&#62;3. Note: Your&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-24-swap-nodes-in-pairs/">花花酱 LeetCode 24. Swap Nodes in Pairs</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, swap every two adjacent nodes and return its head.</p>
<p><strong>Example:</strong></p>
<pre class="crayon:false">Given <code>1-&gt;2-&gt;3-&gt;4</code>, you should return the list as <code>2-&gt;1-&gt;4-&gt;3</code>.</pre>
<p><strong>Note:</strong></p>
<ul>
<li>Your algorithm should use only constant extra space.</li>
<li>You may <strong>not</strong> modify the values in the list&#8217;s nodes, only nodes itself may be changed.</li>
</ul>
<h1><strong>Solution</strong></h1>
<p>Time complexity: O(n)</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 *swapPairs(ListNode *head) {
    if (!head || !head-&gt;next) return head;    

    ListNode d(0);
    d.next = head;
    head = &amp;d;

    while (head &amp;&amp; head-&gt;next &amp;&amp; head-&gt;next-&gt;next) {
      auto n1 = head-&gt;next;
      auto n2 = n1-&gt;next;

      n1-&gt;next = n2-&gt;next;
      n2-&gt;next = n1;

      head-&gt;next = n2;
      head = n1;
    }
    return d.next;
  }
};</pre><p></div><h2 class="tabtitle">Python3</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">class Solution:
  def swapPairs(self, head):
    if not head or not head.next: return head
    dummy = ListNode(0)
    dummy.next = head
    head = dummy
    while head.next and head.next.next:
      n1, n2 = head.next, head.next.next
      n1.next = n2.next
      n2.next = n1
      head.next = n2      
      head = n1
    return dummy.next</pre><p></div></div></p>
<h1>Related Problems</h1>
<ul>
<li><a href="https://zxi.mytechroad.com/blog/divide-and-conquer/leetcode-148-sort-list/">花花酱 LeetCode 148. Sort List</a></li>
<li><a href="https://zxi.mytechroad.com/blog/list/leetcode-203-remove-linked-list-elements/">花花酱 LeetCode 203. Remove Linked List Elements</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/list/leetcode-24-swap-nodes-in-pairs/">花花酱 LeetCode 24. Swap Nodes in Pairs</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-24-swap-nodes-in-pairs/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>
	</channel>
</rss>
