<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Linked List | Haobin Tan</title><link>https://haobin-tan.netlify.app/tags/linked-list/</link><atom:link href="https://haobin-tan.netlify.app/tags/linked-list/index.xml" rel="self" type="application/rss+xml"/><description>Linked List</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Thu, 15 Apr 2021 00:00:00 +0000</lastBuildDate><image><url>https://haobin-tan.netlify.app/media/icon_hu7d15bc7db65c8eaf7a4f66f5447d0b42_15095_512x512_fill_lanczos_center_3.png</url><title>Linked List</title><link>https://haobin-tan.netlify.app/tags/linked-list/</link></image><item><title>Linked List</title><link>https://haobin-tan.netlify.app/docs/cs/algorithm/leetcode/linkedlist/</link><pubDate>Thu, 15 Apr 2021 00:00:00 +0000</pubDate><guid>https://haobin-tan.netlify.app/docs/cs/algorithm/leetcode/linkedlist/</guid><description>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="k">class&lt;/span> &lt;span class="nc">ListNode&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">def&lt;/span> &lt;span class="fm">__init__&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="bp">self&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="n">val&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="mi">0&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nb">next&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="kc">None&lt;/span>&lt;span class="p">):&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="bp">self&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">val&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">val&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="bp">self&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">next&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nb">next&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="draw-it-out">Draw It out!&lt;/h2>
&lt;p>To solve the linked list problems, the most important thing is to draw the linked list and the procedure out. The figure can help you to think and code.&lt;/p>
&lt;h2 id="2-main-types-of-problems">2 Main Types of Problems&lt;/h2>
&lt;ul>
&lt;li>Modification of links&lt;/li>
&lt;li>Concatenation of linked lists&lt;/li>
&lt;/ul>
&lt;h2 id="to-be-noticed">To Be Noticed&lt;/h2>
&lt;h3 id="loops">Loops&lt;/h3>
&lt;p>When modifying links, it is easy to create loop uncarefully. To prevent this problem, &lt;strong>draw the list out&lt;/strong>! With the help of figure, we can immediately notice and avoid loops.&lt;/p>
&lt;h3 id="corner-cases">Corner Cases&lt;/h3>
&lt;p>Common corner cases are&lt;/p>
&lt;ul>
&lt;li>Linked list is empty&lt;/li>
&lt;li>Linked list contains only one node&lt;/li>
&lt;/ul>
&lt;h2 id="techniques">Techniques&lt;/h2>
&lt;h3 id="dummy-head">Dummy Head&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>Add a &lt;code>dummy_head&lt;/code> before &lt;code>head&lt;/code>&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python3" data-lang="python3">&lt;span class="line">&lt;span class="cl">&lt;span class="n">dummy_head&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">ListNode&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">val&lt;/span>&lt;span class="o">=-&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="nb">next&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="n">head&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;/li>
&lt;/ul>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/%E6%88%AA%E5%B1%8F2021-04-20%2000.56.48.png" alt="截屏2021-04-20 00.56.48">&lt;/p>
&lt;ul>
&lt;li>
&lt;p>&lt;code>dummy_head.next&lt;/code> is always the first nodes after all operations&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Advantage of adding a &lt;code>dummy_head&lt;/code> is that we can treat &lt;code>head&lt;/code> as a normal node, which can help us to handle the corner cases easily (e.g. deleting &lt;code>head&lt;/code> node)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Leetcode problems&lt;/p>
&lt;ul>
&lt;li>25&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="fast-and-slow-pointers">Fast and Slow pointers&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>Since linked list does not support random indexing as array, we have to access nodes starting from &lt;code>head&lt;/code>&lt;/p>
&lt;/li>
&lt;li>
&lt;p>We can use &lt;code>fast&lt;/code> and &lt;code>slow&lt;/code> pointers to simulate the random access, e.g.&lt;/p>
&lt;ul>
&lt;li>If we want to access the middle node of the linked list
&lt;ul>
&lt;li>&lt;code>fast&lt;/code> and &lt;code>slow&lt;/code> pointers start from &lt;code>head&lt;/code>.&lt;/li>
&lt;li>At each step, &lt;code>slow&lt;/code> moves one step forward, &lt;code>fast&lt;/code> moves two steps forward&lt;/li>
&lt;li>When &lt;code>fast&lt;/code> reaches the end, &lt;code>slow&lt;/code> reaches the middle&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>If we want to access the n-th node from the end
&lt;ul>
&lt;li>&lt;code>slow&lt;/code> starts from &lt;code>head&lt;/code>, &lt;code>fast&lt;/code> starts from the n-th node&lt;/li>
&lt;li>At each step, &lt;code>slow&lt;/code> and &lt;code>fast&lt;/code> move one step forward&lt;/li>
&lt;li>When &lt;code>fast&lt;/code> reaches the end, &lt;code>slow&lt;/code> reaches n-th node from the end&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Leetcode problems&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://leetcode.com/problems/linked-list-cycle/">141. Linked List Cycle&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://leetcode.com/problems/linked-list-cycle-ii/">142. Linked List Cycle II&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="recursion">Recursion&lt;/h3>
&lt;ul>
&lt;li>
&lt;p>Linked list has the nature of recursion. If we master the idea of recursion, the solution will be suprisingly clean and succinct&lt;/p>
&lt;/li>
&lt;li>
&lt;p>To apply recursion, we just need to consider three questions&lt;/p>
&lt;ul>
&lt;li>&lt;strong>Base case&lt;/strong>: Unter which situation / When should the recursion be ended?
&lt;ul>
&lt;li>In linked list, base cases are empty list or list containing only one node&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;strong>Return value&lt;/strong>: What should be returned to the previous level?&lt;/li>
&lt;li>&lt;strong>Goal of current level&lt;/strong>: What should be done in the current level?&lt;/li>
&lt;/ul>
&lt;blockquote>
&lt;p>Reference: &lt;a href="https://lyl0724.github.io/2020/01/25/1/">三道题套路解决递归问题&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;/li>
&lt;li>
&lt;p>Pre-order traversal vs. post-orderrecursion&lt;/p>
&lt;ul>
&lt;li>
&lt;p>In pre-order traversal, we image that the &lt;strong>previous nodes are already processed&lt;/strong> (we don&amp;rsquo;t care how they are processed)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>In post-order traversal, we image that the &lt;strong>nodes behind are already processed&lt;/strong> (we don&amp;rsquo;t care how they are processed)&lt;/p>
&lt;/li>
&lt;li>
&lt;p>Example: &lt;a href="https://leetcode.com/problems/swap-nodes-in-pairs">Swap nodes in pairs&lt;/a>&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2021-04-23%2022.47.40.png" alt="截屏2021-04-23 22.47.40" style="zoom:67%;" />
&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>
&lt;p>Example: &lt;a href="https://leetcode.com/problems/swap-nodes-in-pairs">24. Swap Nodes in Pairs&lt;/a>&lt;/p>
&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/截屏2021-04-23%2022.51.06.png" alt="截屏2021-04-23 22.51.06" style="zoom:80%;" />
&lt;/li>
&lt;li>
&lt;p>Leetcode problems&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://leetcode.com/problems/swap-nodes-in-pairs">24. Swap Nodes in Pairs&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://leetcode.com/problems/reverse-linked-list/">206. Reverse Linked List&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="sort-nodes-alphabetically-when-modifying-links-or-concatenating-nodes">Sort Nodes Alphabetically When Modifying Links or Concatenating Nodes&lt;/h3>
&lt;p>When modifying links or concatenating nodes, it&amp;rsquo;s easy to get lost or create loops uncarefully. A small trick to avoid these problems is to&lt;/p>
&lt;ul>
&lt;li>Draw out how the list looks like before and after modification&lt;/li>
&lt;li>Mark the order of nodes alphabetically&lt;/li>
&lt;/ul>
&lt;p>Example&lt;/p>
&lt;p>We want to move node 3 to the front of node 2. We draw the &amp;ldquo;before and after&amp;rdquo; out:&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/89404580-5ca4-47b4-b61c-7ba26cf586f3_1618868088.0298142.png" alt="image">&lt;/p>
&lt;p>Then we mark the order of nodes alphabetically&lt;/p>
&lt;p>&lt;img src="https://raw.githubusercontent.com/EckoTan0804/upic-repo/master/uPic/3f51fe85-c465-4656-a9fc-5eed981d3e33_1618868258.3117332.png" alt="image">&lt;/p></description></item></channel></rss>