</> MAANG.io
coding interview ยท 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Fast / Slow Pointers

What is this?

A linked list is a chain of nodes where each one only points to the next โ€” like a treasure hunt of clues you can only follow forward. Since you can't jump to a position, you send two walkers down the chain at once: one fast and one slow. By watching where the slow walker is when the fast one reaches the end, you learn things like the middle of the list or where two chains join, all in a single pass.

flowchart TD A["Fast and Slow Pointers"] --> B["Move Two Walkers"] B --> C["Different Speeds or Fixed Gap"] C --> D["Read Position When Fast Ends"]

๐Ÿ’ก Fun fact: The fast-and-slow trick is also called Floyd's tortoise and hare algorithm, named after Robert W. Floyd; if the two walkers ever land on the same node, the list secretly loops back on itself.

๐Ÿ”“ The 3 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: You can't index a linked list, so you bring the answer to you with a second pointer. Move one pointer faster than the other (or keep a fixed gap behind it), and a single forward pass reveals the midpoint, the nth-from-end, or where two lists join โ€” all in O(1) memory.


Why a second pointer is the whole trick

A forward-only list hides anything that depends on position relative to the end or total length โ€” you'd normally need a first pass to measure. A cleverly-moving second pointer encodes that information while you walk:

Two pointer motions compared: tortoise & hare (fast 2x means slow lands at mid) and fixed gap of n (fast hits end means slow is n from end)

  • Different speeds (fast = 2ร— slow): when fast reaches the end, slow is at the middle (and if there's a cycle, they meet).
  • Fixed gap (fast starts n ahead): when fast falls off, slow sits exactly where the nth-from-end action happens.
  • List-switching (walk Aโ†’B and Bโ†’A): both pointers traverse lenA + lenB, cancelling the length difference so they align.

The three problems

Fast/slow & fixed-gap pointers map to three problems: Middle of the Linked List, Intersection of Two Lists, Remove Nth From End

  • Middle of the Linked List โ€” slow ร—1, fast ร—2; fast at end โ‡’ slow at middle (return the second middle on even length). One pass.
  • Intersection of Two Linked Lists โ€” pointer a does A-then-B, b does B-then-A; after lenA+lenB steps they meet at the join (or both reach None if disjoint). No length math.
  • Remove Nth Node From End โ€” advance fast n nodes, then move both until fast hits the end; slow is just before the target. A dummy head makes removing the head uniform.

The pattern

Variant Setup Lands you at
Different speed fast = 2 ร— slow the midpoint (or cycle meet-point)
Fixed gap fast starts n ahead the nth-from-end
Switch lists a: Aโ†’B, b: Bโ†’A the intersection / None together

The insight: relative motion is position. Half-speed โ‡’ halfway; a gap of n โ‡’ n from the end; equal total distance โ‡’ aligned. Pick the motion that encodes what you need, and one pass does it in O(1) space.


๐Ÿ““ Draw it yourself

  1. Tortoise & hare. Draw a 6-node list; step slow (ร—1) and fast (ร—2) and mark where slow lands when fast exits โ€” the midpoint.
  2. The gap. For Remove Nth, draw two pointers n apart and slide them together until the front leaves the list; the back is one before the target.

Snap photos and embed them with the /host-diagrams skill.


Key takeaways

  • A second pointer replaces a length pass โ€” different speed, fixed gap, or list-switch.
  • Tortoise & hare finds the midpoint (and cycles) in one pass, O(1) space.
  • Fixed gap of n finds the nth-from-end; pair it with a dummy head.
  • Switch-lists cancels the length gap between two lists โ€” no counting.
  • Why interviewers love it: it shows you can extract position/length info from a forward-only structure without extra memory.

Order: Middle of the Linked List โ†’ Intersection of Two Linked Lists โ†’ Remove Nth Node From End.

Intersection of Two Linked Lists

Core idea: Walk pointer a through list A then list B, and pointer b through B then A โ€” both cover exactly lenA + lenB nodes, so they land on the shared node at the same step (or hit None together if the lists never join).


Problem, rephrased

Imagine two people merging onto a single highway from two different on-ramps. One ramp is long, one is short, but once they merge they drive the exact same road, bumper to bumper, to the end. You're handed the starting point of each ramp. Find the precise spot where the two ramps become one shared road โ€” the first node they have in common.

In linked-list terms: you get headA and headB, the heads of two singly linked lists. The lists may merge โ€” from some node onward they are literally the same nodes (same objects in memory), not merely nodes that happen to hold equal values. Return that first shared node, or None if the lists never touch.

The structural fact that makes this special: once two singly linked lists share a node, they share every node after it too. A node has exactly one next, so there's no way to "un-merge." The overlap is always a clean shared suffix โ€” never a partial criss-cross.

Y-shaped lists: A (a1, a2) and B (b1, b2, b3) merge into the shared suffix c1, c2, c3, None; they intersect at c1 by identity

Inputs / outputs

headA headB shared suffix answer
a1โ†’a2โ†’c1โ†’c2โ†’c3 b1โ†’b2โ†’b3โ†’c1โ†’c2โ†’c3 c1โ†’c2โ†’c3 node c1
x1โ†’y1 y1 y1 node y1
p1โ†’p2โ†’p3 q1โ†’q2 none None
None b1โ†’b2 none None

You return a node reference, not a value. Two separate nodes that both hold 5 are not an intersection โ€” the answer is the node both lists physically run through.


Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account โ€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.