Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Linked Lists

Fast & Slow Pointers

O(n) / O(1)

Move one pointer 1 step and another 2 steps; they meet inside a cycle, and slow lands on the middle when fast hits the end.

  1. 1Learnthe lesson(you are here)
  2. 2Guidedwatch it run, line by line(not yet)
  3. 3Practisethe path, or the climb(not yet)
  4. 4Revisereviews on schedule(not yet)
Start the lesson
UnlocksIn-place ReversalFunctional / Successor Graphs
0%
mastery
0/11 solved
IdeaPracticeTemplatePitfalls

Recognise it

The signal

Linked-list cycle or middle in one pass with O(1) space, or a chain x → f(x) that must repeat (Happy Number, Find the Duplicate).

  1. 1Brute force

    Store visited nodes in a hash set.

  2. 2The observation

    O(n) memory: the speed difference guarantees meeting inside a cycle.

  3. 3Why it works

    In a cycle the fast–slow gap shrinks by 1 per step, so they meet; then restart one at head, step both by 1, they meet at the entry.

Reach for it when

  • +A linked list that might have a cycle, or you need its middle in one pass.
  • +O(1) space required (can't use a visited set / hash).
  • +A sequence defined by repeatedly applying a function (Happy Number, Find the Duplicate) — a hidden cycle.

Not when

  • ×A hash set is affordable and you want simpler codeuse Visited set
  • ×You have random access (array) and want the middleuse Index len // 2
  • ×Nodes have several successors, or you need the cycle members of a general graphuse DFS with colouring

Intuition

Two pointers walking the same list at different speeds — slow moves 1 node per step, fast moves 2. It's the trick behind cycle detection and finding the middle in one pass, and it needs O(1) extra space (no visited set).

Read the full intuition

Finding the middle: when fast reaches the end, it has gone twice as far, so slow is exactly halfway. One pass, no length count.

Detecting a cycle (Floyd's tortoise & hare): picture two runners on a circular track — the faster must eventually lap and meet the slower. If fast ever equals slow, there's a loop. If fast hits null, the list ends → no loop. The faster runner closes the gap by exactly 1 node each step, so they're guaranteed to collide inside any cycle.

Finding the cycle's start (a beautiful bonus): after they meet, reset one pointer to the head and move both one step at a time; they meet again exactly at the cycle's entrance. (It falls out of the distance algebra — worth memorizing, not re-deriving in an interview.)

Key idea

The same idea generalizes beyond linked lists: "Happy Number" and "Find the Duplicate Number" become cycle problems where next is "apply this function" — spotting that disguise is the real skill.

Why it costs what it costs

Time O(n). fast traverses the list at most twice before meeting slow or hitting the end.

Space O(1). two pointers, no auxiliary structure — the whole point versus a visited set.

Practice

Next · Foundation

Middle of the Linked List

When fast ends, slow is at the middle (decide which middle for even n).

0/11 solved
  • Middle of the Linked ListFoundationFoundationAmazon, AdobeEasy
  • Linked List CycleApply it · Detect a cycle.Apply itAmazon, MicrosoftEasy
  • Happy NumberOne change · The 'list' is an implicit sequence x -> f(x).One changeAdobe, AppleEasy
  • Linked List Cycle IITwists · Find the entry, not just existence.TwistsAmazon, AdobeMedium
  • Palindrome Linked ListCombine · Find the middle, reverse the second half, compare.CombineAmazon, AdobeEasy
  • Reorder ListCombine · Middle + reverse + interleave merge.CombineAmazon, MetaMedium
  • Find the Duplicate NumberMastery checkMastery checkMedium
  • Remove Nth Node From End of ListAmazon, MetaMedium
  • Sort ListAmazon, AdobeMedium
  • Intersection of Two Linked ListsAmazon, MicrosoftEasy
  • Throw the Ball (k up to 1e12)OracleOAMedium

Pitfalls

Classic bugs

  • !Guard is while fast and fast.next — checking only fast crashes on fast.next.next.
  • !slow=fast=head gives the second middle of an even list; start fast=head.next for the first.
  • !Compare node identity (is/===), not values — different nodes can hold equal values.
  • !The meeting point is not the cycle start: run phase two, moving both one step (not fast at 2×).
  • !In Happy Number / Find the Duplicate, next is f(x) or nums[x], not a .next pointer — model it that way.
Go deepervariants and techniques · 1 combinations · resources

Variants and techniques

  • Cycle detectionMeet ⇒ cycle, fast hits null ⇒ none. Linked List Cycle.
  • Find middleslow lands on the midpoint when fast ends. Middle of the Linked List.
  • Cycle entranceAfter meeting, reset one to head, advance both by 1, meet at the start. Linked List Cycle II.
  • Function iterationTreat x→f(x) as 'next'; a repeat means a cycle. Happy Number, Find the Duplicate Number.
  • Palindrome listFind middle, reverse the second half, compare. Palindrome Linked List.

Combinations

  • Fast & Slow Pointers+In-place Reversal

    palindrome list, reorder list

    Fast & Slow Pointers:
    finds the middle
    In-place Reversal:
    reverses the second half

    Palindrome Linked ListReorder List

Resources

  • Linked List explore cardLeetCode · course
  • Linked list cheatsheetTech Interview Handbook · article
  • Linked list visualizerVisuAlgo · tool
  • Cycle detection (Floyd/Brent)Wikipedia · article
  • NeetCode RoadmapNeetCode · sheet
Difference Array & Line SweepIn-place Reversal
TodayReviewPatternsProblems

Template · clean C++, Java and Python to revise from

Start the free 7-day trial