
Fast and Slow Pointer Technique in Linked List: Cycle Detection, Proof, and Complete Explanation
π Before We StartTry these problems (optional but helpful):https://leetcode.com/problems/linked-list-cycle/https://leetcode.com/problems/linked-list-cycle-ii/π€ Letβs Talk Honestlyβ¦When you first learn this technique, youβre told:π βSlow moves 1 step, fast moves 2 stepsβπ βIf they meet β cycle existsβBut your brain asks:β Why 2 steps?β Why do they meet at all?β Why does resetting pointer find cycle start?β Is this magic or logic?π Letβs answer each doubt one by one.π§© Doubt 1: Why do we even use two pointers?β Question:Why not just use one pointer?β Answer:With one pointer:You can only move forwardYou cannot detect loops efficientlyπ Two pointers create a relative motionThat relative motion is the key.π§© Doubt 2: Why fast = 2 steps and slow = 1 step?β Question:Why exactly 2 and 1?β Answer:We need:Fast speed > Slow speedSo that:π Fast catches up to slowπ§ Think like this:If both move same speed:Slow β 1 stepFast β 1 stepπ They will NEVER meet βIf:Slow β 1 stepFast β 2 stepsπ Fast gains 1 node every stepπ₯ Key Insight:Relative speed = fast - slow = 1π This means fast is closing the gap by 1 node every stepπ§© Doubt 3: Why do they ALWAYS meet in a cycle?β Question:Okay, fast is fasterβ¦ but why guaranteed meeting?π§ Imagine a Circular TrackInside a cycle, the list behaves like:Circle of length = Ξ»Now:Slow moves 1 stepFast moves 2 stepsπ Gap BehaviorEach step:Gap = Gap - 1Because fast is catching up.Eventually:Gap = 0π They meet π―π‘ Simple AnalogyTwo runners on a circular track:One is fasterOne is slowerπ Faster runner will lap and meet slower runnerπ§© Doubt 4: What if there is NO cycle?β Question:Why does this fail without cycle?β Answer:If no cycle:List ends β fast reaches nullπ No loop β no meetingπ§© Doubt 5: Where do they meet?β Question:Do they meet at cycle start?β Answer:No, not necessarily.They meet somewhere inside the cycleπ§© Doubt 6: Then how do we find the cycle start?Now comes the most important part.π― SetupLetβs define:a = distance from head to cycle startb = distance from cycle start to meeting pointc = remaining cycleCycle length:Ξ» = b + cπ§ What happens when they meet?Slow distance:a + bFast distance:2(a + b)Using relation:2(a + b) = a + b + kΞ»Solve:a + b = kΞ»=> a = kΞ» - b=> a = (k-1)Ξ» + (Ξ» - b)π₯ Final Meaninga = distance from meeting point to cycle startπ₯ BIG CONCLUSIONπ Distance from head β cycle startπ = Distance from meeting point β cycle startπ§© Doubt 7: Why resetting pointer works?β Question:Why move one pointer to head?β Answer:Because:One pointer is a away from startOther is also a away (via cycle)π Move both 1 step:They meet at:Cycle Start π―π VisualizationHead β β β Cycle Start β β Meeting Point β β back to StartBoth pointers:One from headOne from meeting pointπ Same distance β meet at startπ§© Doubt 8: Can we use fast = 3 steps?β Question:Will this work?β Answer:Yes, BUT:Math becomes complexHarder to reasonNo extra benefitπ So we use simplest:2 : 1 ratioπ§ Final Mental ModelThink in 3 steps:1. Different SpeedsFast moves faster β gap reduces2. Circular StructureCycle β positions repeat3. Guaranteed MeetingFinite positions + relative motion β collisionπ§© TEMPLATE 1: Detect CycleListNode slow = head;ListNode fast = head;while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; if(slow == fast){ return true; }}return false;π§© TEMPLATE 2: Find Cycle StartListNode slow = head;ListNode fast = head;while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; if(slow == fast){ slow = head; while(slow != fast){ slow = slow.next; fast = fast.next; } return slow; }}return null;π§© TEMPLATE 3: Find Middle of Linked Listβ ProblemFind the middle node of a linked list.π§ IntuitionFast moves twice as fast:When fast reaches end β slow reaches halfπ Slow = middleπ» CodeListNode slow = head;ListNode fast = head;while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next;}return slow;β οΈ Even Length Case1 β 2 β 3 β 4 β 5 β 6π Returns 4 (second middle)β How to Get First Middle?while(fast.next != null && fast.next.next != null){ slow = slow.next; fast = fast.next.next;}return slow;π§© Where Else This Technique Is Used?Detect cycleFind cycle startFind middle nodeCheck palindrome (linked list)Split list (merge sort)Intersection problemsβοΈ Time & Space ComplexityTime: O(n)Space: O(1)β Common MistakesForgetting fast.next != nullThinking meeting point = cycle start βNot resetting pointer properlyπ§ Final Mental ModelThink in 3 steps:1. Speed DifferenceFast moves faster β gap reduces2. Circular NatureCycle β repeated positions3. Guaranteed MeetingFinite nodes + relative motion β collisionπ₯ One Line to RememberFast catches slow because it reduces the gap inside a loop.π ConclusionNow you understand:β Why fast moves fasterβ Why they meetβ Why meeting proves cycleβ Why reset gives cycle startβ How to find middle using same logicπ This is not just a trickβ¦Itβs a mathematical guarantee based on motion and cycles.π‘ Master this once, and youβll solve multiple linked list problems easily.






