<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Programming on Xing Lin</title>
    <link>https://xinglin.github.io/tags/programming/</link>
    <description>Recent content in Programming on Xing Lin</description>
    <generator>Hugo</generator>
    <language>en</language>
    <copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright>
    <lastBuildDate>Fri, 12 Jun 2026 23:23:20 -0700</lastBuildDate>
    <atom:link href="https://xinglin.github.io/tags/programming/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Detect cycle in a singly linked list</title>
      <link>https://xinglin.github.io/detect-cycle/</link>
      <pubDate>Thu, 10 Dec 2020 00:00:00 +0000</pubDate>
      <guid>https://xinglin.github.io/detect-cycle/</guid>
      <description>&lt;p&gt;Question: given a singly linked list, detect whether it contains a cycle.&lt;/p&gt;&#xA;&lt;p&gt;Solution: use two pointers: one slow and one fast pointer.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;since fast pointer is moving faster, both pointers can&amp;rsquo;t meet before they enter the cycle. Their meeting point must be part of&#xA;the cycle.&lt;/li&gt;&#xA;&lt;li&gt;In order for them to meet, it must be fast pointer catching up with the slow pointer.&lt;/li&gt;&#xA;&lt;li&gt;They are guarantteed to meet because at every tick, fast pointer is 1 step closer to the slower pointer.&#xA;So, eventually they will meet. By the point they meet the first time, fast pointer travels exactly k more loops than the slow pointer.&lt;/li&gt;&#xA;&lt;li&gt;When they meet, fast pointer travels 2T nodes. slow pointer travels T nodes. Their difference is k&lt;em&gt;L, length of loop.&#xA;So, 2T = T + k&lt;/em&gt;L, which means T = k&lt;em&gt;L. So, by the time they meet, slow pointer travels exactly k&lt;/em&gt;L nodes.&lt;/li&gt;&#xA;&lt;li&gt;A: The distance from the start of the list to the start of the cycle.&lt;/li&gt;&#xA;&lt;li&gt;B: The distance from the start of the cycle to the meeting point.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;&#xA;&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;&#xA;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1&#xA;&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2&#xA;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&#xA;&lt;td class=&#34;lntd&#34;&gt;&#xA;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;A + B = k*L. &#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;A = (k-1)*L + L - B. &#xA;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&#xA;&lt;/div&gt;&#xA;&lt;/div&gt;&lt;ul&gt;&#xA;&lt;li&gt;And the difference from the meeting point to the start of the cycle is also L - B.&#xA;So, both pointers will meet at the start of the cycle at the same time.&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
    </item>
  </channel>
</rss>
