INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS
01
00/10
01 / COVER STEP 06 · LINKED LIST
INVARIANT · STEP 06 · DECK 2 OF 3
TEN MEDIUMS, SIX TRICKS

Almost every medium linked-list problem is one of a handful of pointer tricks: a slow pointer and a fast one, a three-pointer reversal, Floyd's tortoise-and-hare for cycles, two pointers held a fixed gap apart, a rearrange-and-stitch, or a tandem walk of two lists. Learn the six and the ten problems collapse into variations you can write from memory — each animated one pointer at a time, with the trap that catches most people called out.

10Problems
6Patterns
10Units
10Lectures
← → ↑ ↓  or  W A S D  navigate SPACE next   DOUBLE-CLICK to advance I index   G goto problem   P predict H hide solutions   T close video   F fullscreen
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
02 / START HERE READ THIS ONE FIRST

HOW THIS DECK WORKS

This is not a list of problems. It is 10 learning units, and every one runs the same four beats. Go in order and the problems stop feeling random.

01 INTROWhat the concept is, and what to watch for
02 VIDEOThe lecture, full-width in theatre mode
03 DRILLS2–4 questions checking the lecture landed
04 PROBLEMSThe sheet problems that concept unlocks

Moving around

← ↑Back a slide — or A / W → ↓Forward — or D / S / Space 2×clickDouble-click the right side to advance, left to go back. A single click never moves the deck. IThe index: every problem, clickable, with your progress GJump straight to a problem by its number FFullscreen

While you study

HHide solutions — blurs code and steps so you try first PPredict mode: call the next step before the animation plays it TClose the video — Esc works too ☐ ★Mark solved, or star to revisit. Both are saved automatically.

10 PROBLEMS · EVERY ONE LINKS TO LEETCODE TO SOLVE

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
03 / INDEX PRESS I FROM ANYWHERE

INDEX — ALL 10 PROBLEMS

Click any problem to jump straight to it. Solved ones turn green. Press I from anywhere to come back here. The number is the sheet's row, not your position — the deck teaches in the order that builds, so you will meet them slightly out of numerical sequence.

FAST & SLOW · 02
REVERSE IN PLACE · 02
FLOYD'S CYCLE · 02
THE GAP TRICK · 01
REARRANGE & STITCH · 01
TWO LISTS · 02
SOLVED HAS A LEETCODE LINK
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
04 / SIGNALS WHEN YOU SEE X, REACH FOR Y

WHICH POINTER TRICK, AND WHY

Ten problems, six tricks. The cards below are the phrases in a statement that pick the trick for you — hearing which one a problem is asking for is the whole skill.

“THE MIDDLE” / “HALVE THE LIST” / “n-th FROM THE MIDDLE”

one pointer at double speed reaches the end as the other reaches the middle

FAST & SLOW pointersO(n) time · O(1) space
“REVERSE” / “PALINDROME” / “REORDER”

flipping next-pointers in place is the engine; palindrome composes it

THREE-POINTER REVERSE (prev / curr / next)O(n) time · O(1) space
“CYCLE” / “LOOP” / “DOES IT TERMINATE”

a hash set works but costs O(n) space; two speeds cost nothing

FLOYD'S TORTOISE & HAREO(n) time · O(1) space
“n-th NODE FROM THE END”

you cannot index from the end — manufacture a fixed gap instead

TWO POINTERS, GAP OF n (+ dummy head)O(n) one pass · O(1) space
“GROUP / PARTITION BY A RULE, KEEP ORDER”

split into separate chains as you walk, then stitch them back

REARRANGE & STITCH (odd/even, segregate)O(n) time · O(1) space
“TWO LISTS: INTERSECTION / MERGE / ADD”

walk them in tandem, building the answer behind a dummy head

TANDEM WALK + DUMMY NODEO(n+m) time · O(1) space
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
05 / CONSTRAINTS BEFORE YOU WRITE A LINE

READ THE ANSWER OFF THE CONSTRAINTS

Every one of these runs in O(n) time; the game is O(1) space — trading a hash set or an array for a couple of pointers. The gold rows are the pointer methods this deck teaches.

n ≤
BUDGET
WHAT THAT BUYS YOU
copy into an array / stack
O(n) space
the brute for reverse, palindrome, nth — correct but wasteful
hash set of visited nodes
O(n) space
cycle detection the easy way — Floyd deletes the space cost
fast & slow pointers
O(1) space
middle · cycle · palindrome — the signature space win
three-pointer reverse
O(1) space
flip next-pointers in place, no new nodes
dummy + two pointers
O(1) space
nth-from-end · add · merge — one clean pass

TIME IS O(n) FOR ALL · THE WIN IS SPACE · POINTERS BEAT AN ARRAY OR A HASH SET

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
06 / ROADMAP WHAT YOU ARE ABOUT TO DO

THE WHOLE RUN, 10 UNITS

Ten mediums, six tricks. Fast/slow, reverse, and Floyd are the load-bearing three; the gap, the rearrange, and the tandem walk finish the set. The hards (deck 3) compose these.

UNIT 01

The Middle by Fast & Slow

▶ 14:372 DRILLS1 PROBLEM
UNIT 02

Reverse a Linked List

▶ 32:422 DRILLS1 PROBLEM
UNIT 03

Detect a Cycle (Floyd)

▶ 20:262 DRILLS1 PROBLEM
UNIT 04

Where the Cycle Begins

▶ 22:422 DRILLS1 PROBLEM
UNIT 05

Palindrome — Reverse the Half

▶ 20:022 DRILLS1 PROBLEM
UNIT 06

Remove Nth — the Gap Trick

▶ 16:232 DRILLS1 PROBLEM
UNIT 07

Delete the Middle Node

▶ 16:362 DRILLS1 PROBLEM
UNIT 08

Odd / Even Rearrange

▶ 24:052 DRILLS1 PROBLEM
UNIT 09

Intersection of Two Lists

▶ 32:052 DRILLS1 PROBLEM
UNIT 10

Add Two Numbers

▶ 14:482 DRILLS1 PROBLEM
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
07 / WARMUP LOAD THE THREE CORE TRICKS FIRST · 1 OF 2

THREE THINGS WORTH HAVING IN MEMORY

DRILL 01 · RECALL

Why does a fast pointer (2 steps) and a slow pointer (1 step), both from the head, leave slow at the middle when fast reaches the end?

Double speed means half the distance. In the same number of steps, fast moves 2k nodes while slow moves k; when fast has traversed all n nodes, k = n/2, so slow sits at the midpoint. No length count, no second pass. The only subtlety is the loop guard — fast && fast->next versus fast->next && fast->next->next — which decides whether an even-length list gives you the first or second middle.

DRILL 02 · BUG

This in-place reversal returns a list of length 1. Which line is missing or misplaced?

while (curr) {
    curr->next = prev;            // flipped before saving next!
    prev = curr;
    curr = curr->next;           // curr->next is now prev…
}

You must save next before you flip. The instant curr->next = prev runs, the forward link is gone, so the later curr = curr->next follows the pointer you just redirected — straight back to prev — and the walk collapses. The fix is the first line of the dance: ListNode* next = curr->next; then flip, then advance prev = curr; curr = next;. Every reverse-based problem in this deck depends on that ordering.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
08 / WARMUP LOAD THE THREE CORE TRICKS FIRST · 2 OF 2

THREE THINGS WORTH HAVING IN MEMORY

DRILL 01 · RECALL

Cycle detection with a hash set of visited nodes is O(n) time. What does Floyd's tortoise-and-hare buy over it?

O(1) space instead of O(n). Both approaches are linear in time, but the hash set stores every node it has seen; Floyd's method carries only two pointers. A fast pointer moving two steps and a slow one moving one will, inside any loop, close the gap by one node per step until they collide — a meeting that is impossible on a null-terminated list. Interviewers ask for the O(1) solution specifically because the hash set is the obvious one; the tortoise and hare is the one that shows you know the pointer trick.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
09 / INTRO UNIT 01 · The Middle by Fast & Slow

UNIT 01 — The Middle by Fast & Slow

The fast & slow pattern: two pointers from the head, one moving one node per step and one moving two. Because fast covers twice the ground, the moment it runs off the end slow is standing on the middle — the whole list halved in a single pass, no length count, O(1) space.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU FIND THE MIDDLE IN ONE PASS, WITHOUT COUNTING THE LENGTH?

fast & slowDOUBLE SPEEDMIDDLEone passnull GUARD
WHAT TO WATCH FOR
  • 01slow += 1, fast += 2 — fast ALWAYS COVERS DOUBLE THE DISTANCE
  • 02GUARD: while (fast && fast->next) — BOTH CHECKS OR THE DOUBLE STEP HITS null
  • 03WHEN fast REACHES THE END, slow IS AT n/2 — THE MIDDLE
  • 04THE GUARD DECIDES FIRST-vs-SECOND MIDDLE ON EVEN-LENGTH LISTS
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
10 / VIDEO UNIT 01 · The Middle by Fast & Slow

L13. Find the middle element of the LinkedList | Multiple Approaches

STRIVER A2Z
The Middle by Fast & Slow
RUNTIME 14:37
AFTER THIS → 2 DRILLS · PROBLEM #01
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
11 / DRILL UNIT 01 · The Middle by Fast & Slow

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

Why is the loop condition while (fast && fast->next) rather than just while (fast)?

The double step reads fast->next->next. Evaluating that requires fast to be non-null and fast->next to be non-null; on an even-length list fast can land exactly on the last node, where fast->next is null and the second hop would dereference it. Guarding both is what keeps the fast pointer safe, and the precise guard you choose also controls which of the two middles you get.

DRILL 02 · TRACE

On 1 → 2 → 3 → 4 → 5, after the fast/slow loop, where is slow?

Value 3, index 2. Trace it: slow 1→2→3 while fast 1→3→5. When fast is on 5, fast->next is null, the loop stops, and slow is on 3 — dead centre of the five nodes. For an even list like 1→2→3→4 the same guard leaves slow on 3 (the second middle); switching the guard would give 2. The MECHANISM slide steps the two pointers and pulses the middle when it settles.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
12 / MECHANISM UNIT 01 · FASTSLOW · CODE MIRRORED

TWO SPEEDS, AND THE MIDDLE FALLS OUT

Send slow one node at a time and fast two. Because fast covers exactly double the distance, when it runs off the end slow has covered half — it is standing on the middle, found in a single pass with no length count. The loop guard fast && fast->next is what keeps the double-step from dereferencing null, and choosing that guard (versus fast->next && fast->next->next) decides which node you get on an even-length list.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
13 / PROBLEM #01 · FAST-SLOW · EASY

Middle of the Linked List

EASY fast-slow ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

“Return the middle node” (or split the list in half). Anything asking for the midpoint of a singly list in one pass is the fast/slow pattern — no length count, O(1) space.

INTUITION

Two pointers from the head; fast moves two nodes for every one that slow moves. When fast falls off the end, slow is exactly halfway. For even length, the guard fast && fast->next returns the second of the two middles (LeetCode's choice).

STEPS
  1. slow = head, fast = head
  2. While fast and fast->next are non-null:
  3. slow = slow->next; fast = fast->next->next
  4. When the loop ends, slow is the middle
  5. Return slow
BRUTEO(n) + O(n)
OPTIMALO(n)
↕ SCROLL
// Fast covers 2x the distance, so when it hits the end slow is halfway.
ListNode* middleNode(ListNode* head) {
    ListNode* slow = head;
    ListNode* fast = head;
    while (fast && fast->next) {
        slow = slow->next;          // +1
        fast = fast->next->next;    // +2
    }
    return slow;                    // the (second) middle
}
TIMEO(n)a single pass; fast covers the list once
SPACEO(1)two pointers, no length array
TRAP

The null guard, and which middle you return. fast->next->next dereferences fast->next, so both fast and fast->next must be checked or an even-length list crashes. That same guard determines whether you land on the first or second middle — problems built on this (delete-middle, reorder) care which, so know your convention.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #01 · MIDDLE OF THE LINKED LIST

L13. Find the middle element of the LinkedList | Multiple Approaches

The walkthrough for #01 Middle of the Linked List. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L13. Find the middle element of the LinkedList | Multiple Approaches
RUNTIME 14:37
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
14 / INTRO UNIT 02 · Reverse a Linked List

UNIT 02 — Reverse a Linked List

Reversing a singly linked list in place is the three-pointer dance that half the hard problems reuse. Carry prev (starts null), curr, and a temporary next. Each step: save next, point curr->next back at prev, then slide all three forward. prev ends on the old tail — the new head.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU FLIP EVERY POINTER WITHOUT LOSING THE REST OF THE LIST?

prev / curr / nextSAVE then FLIPin placenew head = old tailO(1) space
WHAT TO WATCH FOR
  • 01SAVE next = curr->next BEFORE ANYTHING — THE FLIP DESTROYS THE FORWARD LINK
  • 02FLIP: curr->next = prev · SLIDE: prev = curr; curr = next
  • 03RETURN prev (THE OLD TAIL), NOT THE OLD head
  • 04THE RECURSIVE VERSION IS THE SAME MOVE UNWOUND FROM THE BACK
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
15 / VIDEO UNIT 02 · Reverse a Linked List

L9. Reverse a LinkedList | Iterative and Recursive

STRIVER A2Z
Reverse a Linked List
RUNTIME 32:42
AFTER THIS → 2 DRILLS · PROBLEM #02
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
16 / DRILL UNIT 02 · Reverse a Linked List

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

In the three-pointer reversal, what is returned at the end, and why?

Return prev. As curr advances, prev trails one behind, and when curr falls off the end to null, prev is sitting on the last real node — the old tail, which in the reversed list is the head. The original head variable now points at what became the tail, so returning it yields a truncated list. This “return prev” is the single most common slip in reversal-based problems.

DRILL 02 · TRANSFER

Reverse-nodes-in-k-group and reorder-list are “hard” problems. What makes them tractable once this unit is automatic?

They are sublist reversals with reconnection. Reverse-in-k-group runs this dance on each window of k nodes and links the reversed chunks together; reorder-list reverses the back half and interleaves. The core loop is identical — save, flip, slide — and the only new work is tracking the boundary nodes so the reversed segment stitches back cleanly. That is exactly why reversal is taught as a fundamental: the deck-3 hards are compositions of it.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
17 / MECHANISM UNIT 02 · REVERSE · CODE MIRRORED

THREE POINTERS, ONE FLIP PER NODE

Reversing a singly list in place is the dance every hard problem reuses. Hold prev (starts null), curr, and a saved next. Each step: save next = curr->next, flip curr->next = prev, then slide prev = curr; curr = next. Miss the save and you lose the rest of the list the instant you flip. prev walks to the old tail and is the new head — return it, not the old head.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
18 / PROBLEM #02 · REVERSE · EASY

Reverse Linked List

EASY reverse ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

Reverse the list” — the most reused operation in the topic. Iterative three-pointer walk in O(1) space; the recursive form is the same flip unwound from the tail.

INTUITION

Hold prev (null), curr (head), and a temporary next. Each step: save next = curr->next, flip curr->next = prev, then slide prev = curr; curr = next. prev ends on the old tail — return it.

STEPS
  1. prev = null, curr = head
  2. While curr is non-null:
  3. next = curr->next (save the rest)
  4. curr->next = prev (flip)
  5. prev = curr; curr = next (slide)
  6. Return prev — the new head
BRUTEO(n) space
OPTIMALO(n)
↕ SCROLL
// Save next, flip curr->next to prev, slide all three forward.
ListNode* reverseList(ListNode* head) {
    ListNode* prev = nullptr;
    ListNode* curr = head;
    while (curr) {
        ListNode* next = curr->next;   // save the rest FIRST
        curr->next = prev;             // flip the link
        prev = curr;                   // slide prev
        curr = next;                   // slide curr
    }
    return prev;                       // old tail is the new head
}
TIMEO(n)one pass, one flip per node
SPACEO(1)three pointers; iterative, no call stack
TRAP

Flipping before saving, or returning the old head. If you set curr->next = prev before capturing next, the forward link is gone and the walk collapses. And the answer is prev — it reached the old tail (the new head); returning the original head yields a single node.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #02 · REVERSE LINKED LIST

L9. Reverse a LinkedList | Iterative and Recursive

The walkthrough for #02 Reverse Linked List. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L9. Reverse a LinkedList | Iterative and Recursive
RUNTIME 32:42
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
19 / INTRO UNIT 03 · Detect a Cycle (Floyd)

UNIT 03 — Detect a Cycle (Floyd)

Floyd's tortoise and hare detects a cycle with two pointers and no extra memory. Run slow one step and fast two. On a null-terminated list fast simply reaches the end; but if there is a loop, fast enters it and gains one node on slow every step, so it eventually laps and collides with it. A meeting is proof of a cycle.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU DETECT A LOOP IN O(1) SPACE, WITHOUT A SET OF VISITED NODES?

tortoise & hareCOLLISIONno hash setO(1) spacethe gap shrinks
WHAT TO WATCH FOR
  • 01slow += 1, fast += 2 — INSIDE A LOOP THE GAP SHRINKS BY ONE EACH STEP
  • 02IF fast OR fast->next IS null, THERE IS NO CYCLE — IT ESCAPED
  • 03IF slow == fast, THEY COLLIDED — A CYCLE EXISTS
  • 04THE HASH-SET SOLUTION IS O(n) SPACE; THIS IS THE O(1) THE PROBLEM WANTS
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
20 / VIDEO UNIT 03 · Detect a Cycle (Floyd)

L14. Detect a loop or cycle in LinkedList | With proof and Intuition

STRIVER A2Z
Detect a Cycle (Floyd)
RUNTIME 20:26
AFTER THIS → 2 DRILLS · PROBLEM #03
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
21 / DRILL UNIT 03 · Detect a Cycle (Floyd)

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

Why must a fast pointer (moving 2) and a slow pointer (moving 1) meet if there is a cycle?

The gap shrinks by one each step. Once both pointers are within the loop, think in the loop's frame: fast advances 2 and slow advances 1, so their separation decreases by 1 every step. A non-negative integer that strictly decreases must hit 0 — the collision — and it cannot “jump over” because the step difference is exactly 1. On a list with no loop, fast escapes to null first, so meeting is a sound proof of a cycle.

DRILL 02 · TRANSFER

A hash-set solution stores every visited node and flags the first repeat. Both are O(n) time — when would you still prefer it?

Floyd wins for pure detection; a set trades space for simplicity. The tortoise-and-hare gives O(1) space and, with a second phase, the loop's start — so it is the interview-expected answer. A visited-set is easier to reason about and generalises when you must attach information to each node or handle concurrent mutation, at the cost of O(n) memory. Knowing precisely what the O(1) method buys (and what it costs in cleverness) is the point of contrasting them.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
22 / MECHANISM UNIT 03 · CYCLE · CODE MIRRORED

TORTOISE AND HARE — MEETING PROVES A CYCLE

Floyd's trick: run slow (+1) and fast (+2). On a straight list fast simply runs off the end. If there is a loop, fast enters it first and gains one node on slow each step, so it eventually laps and collides with it — a meeting is impossible without a cycle. O(1) space, unlike a hash set of visited nodes. The curved arrow is the tail's next pointing back into the list.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
23 / PROBLEM #03 · FLOYD · EASY

Linked List Cycle

EASY floyd ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

“Does the list have a cycle” / “is it infinite”. The O(1)-space answer is Floyd's tortoise and hare; the hash-set answer is O(n) space and usually not what is wanted.

INTUITION

Run slow one step and fast two. If fast reaches null there is no loop; if it ever equals slow, a cycle exists — inside a loop the fast pointer closes the one-node gap each step and must collide.

STEPS
  1. slow = head, fast = head
  2. While fast and fast->next are non-null:
  3. slow = slow->next; fast = fast->next->next
  4. if slow == fast, return true (they collided)
  5. If the loop exits, return false (fast escaped to null)
BRUTEO(n) space (hash set)
OPTIMALO(n)
↕ SCROLL
// Two speeds: a meeting can only happen inside a loop.
bool hasCycle(ListNode* head) {
    ListNode* slow = head;
    ListNode* fast = head;
    while (fast && fast->next) {
        slow = slow->next;          // +1
        fast = fast->next->next;    // +2
        if (slow == fast) return true;   // collision => cycle
    }
    return false;                   // fast reached null => no cycle
}
TIMEO(n)fast traverses at most ~2n nodes before meeting or escaping
SPACEO(1)two pointers, no visited-set
TRAP

Comparing values instead of nodes, and the null guard. Compare the pointers themselves (slow == fast), not slow->val == fast->val — equal values do not mean the same node. And the loop needs fast && fast->next so the double step never dereferences null on an acyclic list.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #03 · LINKED LIST CYCLE

L14. Detect a loop or cycle in LinkedList | With proof and Intuition

The walkthrough for #03 Linked List Cycle. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L14. Detect a loop or cycle in LinkedList | With proof and Intuition
RUNTIME 20:26
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
24 / INTRO UNIT 04 · Where the Cycle Begins

UNIT 04 — Where the Cycle Begins

Finding where a cycle begins is the elegant sequel to detecting it. After the tortoise and hare meet, reset one pointer to head, leave the other at the meeting point, and advance both one step at a time. They meet again exactly at the loop's entrance — a consequence of a short distance identity, not a coincidence.

THE QUESTION THIS LECTURE ANSWERS

ONCE YOU KNOW A LOOP EXISTS, HOW DO YOU FIND THE NODE WHERE IT STARTS?

loop startreset to headsingle-step bothdistance identitytwo phases
WHAT TO WATCH FOR
  • 01PHASE 1: DETECT THE CYCLE (tortoise/hare meet somewhere inside it)
  • 02PHASE 2: RESET slow TO head, KEEP fast AT THE MEETING POINT
  • 03STEP BOTH BY ONE — THEY MEET AT THE LOOP'S START
  • 04THE MATH: dist(head → start) == dist(meet → start), MODULO THE LOOP
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
25 / VIDEO UNIT 04 · Where the Cycle Begins

L17. Find the starting point of the Loop/Cycle in LinkedList | Multiple Approaches

STRIVER A2Z
Where the Cycle Begins
RUNTIME 22:42
AFTER THIS → 2 DRILLS · PROBLEM #04
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
26 / DRILL UNIT 04 · Where the Cycle Begins

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

In phase 2 (finding the loop's start), how fast do the two pointers move?

Both move one step at a time. Phase 2 is a different walk from phase 1: reset one pointer to head, leave the other where they collided, and single-step them together. The distances work out so that they converge precisely at the loop's entry node. Keeping the fast pointer at double speed here is the classic bug — it overshoots the start. The two phases use the same pointers with different step sizes.

DRILL 02 · RECALL

Intuitively, why does resetting one pointer to the head and single-stepping land both at the loop's start?

Two equal distances, so equal-speed walks meet. Let the distance from the head to the loop start be a, and let the tortoise and hare meet at distance b into the loop; the algebra of “fast went twice as far” forces a to equal the remaining loop distance from the meeting point back to the start. So a pointer from head and a pointer from the meeting point, both moving one step, cover the same distance and collide at the entrance. You do not need to reproduce the proof under pressure — you need to remember the two-phase recipe and that phase 2 is single-step.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
27 / MECHANISM UNIT 04 · CYCLESTART · CODE MIRRORED

MEET, RESET ONE POINTER, WALK TO THE START

Detecting the loop is half the job; finding where it begins is the elegant half. After the tortoise and hare meet, reset one pointer to head, leave the other at the meeting point, and advance both one step at a time — they meet again exactly at the loop's entrance. The reason is a short distance argument: the gap from head to the start equals the gap from the meeting point to the start, modulo the loop length.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
28 / PROBLEM #04 · FLOYD · MED

Linked List Cycle II

MED floyd ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

“Return the node where the cycle begins” (or null). Detection plus a second phase: reset one pointer to the head and single-step both to the loop's entrance.

INTUITION

Phase 1 is Floyd detection — get the tortoise and hare to meet. Phase 2: move one pointer back to head, keep the other at the meeting point, and advance both by one; they meet at the loop's start because the head-to-start distance equals the meet-to-start distance around the loop.

STEPS
  1. Run fast/slow until they meet (else return null — no cycle)
  2. Set slow = head; keep fast at the meeting point
  3. While slow != fast: slow = slow->next; fast = fast->next (both +1)
  4. They meet at the loop's start
  5. Return slow
BRUTEO(n) space (hash set)
OPTIMALO(n)
↕ SCROLL
// Detect, then reset one pointer to head and single-step both.
ListNode* detectCycle(ListNode* head) {
    ListNode* slow = head;
    ListNode* fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {                 // phase 1: meeting point
            slow = head;                    // phase 2: reset one pointer
            while (slow != fast) {          // step BOTH by one
                slow = slow->next;
                fast = fast->next;
            }
            return slow;                    // the loop's start
        }
    }
    return nullptr;                         // no cycle
}
TIMEO(n)two linear phases
SPACEO(1)two pointers
TRAP

Stepping fast by two in phase 2, or forgetting the no-cycle exit. Phase 2 must advance both pointers one node at a time — keeping the hare at double speed overshoots the start. And if fast reaches null, there is no cycle: return null rather than entering phase 2.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #04 · LINKED LIST CYCLE II

L17. Find the starting point of the Loop/Cycle in LinkedList | Multiple Approaches

The walkthrough for #04 Linked List Cycle II. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L17. Find the starting point of the Loop/Cycle in LinkedList | Multiple Approaches
RUNTIME 22:42
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
29 / INTRO UNIT 05 · Palindrome — Reverse the Half

UNIT 05 — Palindrome — Reverse the Half

Checking a linked list for a palindrome in O(1) space is a composition of three fundamentals: find the middle with fast/slow, reverse the second half in place, then walk one pointer from the front and one from the reversed back, comparing values. All pairs match ⇒ palindrome. The clearest proof that mediums are fundamentals stacked together.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU CHECK A PALINDROME WITHOUT COPYING THE LIST INTO AN ARRAY?

compose primitivesreverse the halftwo-pointer compareO(1) spacerestore
WHAT TO WATCH FOR
  • 01PHASE 1 — FAST/SLOW TO THE MIDDLE
  • 02PHASE 2 — REVERSE THE SECOND HALF IN PLACE
  • 03PHASE 3 — COMPARE FRONT POINTER WITH REVERSED-HALF POINTER, INWARD
  • 04OPTIONAL: REVERSE THE HALF BACK TO LEAVE THE LIST UNCHANGED
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
30 / VIDEO UNIT 05 · Palindrome — Reverse the Half

L10. Check if a LinkedList is Palindrome or Not | Multiple Approaches

STRIVER A2Z
Palindrome — Reverse the Half
RUNTIME 20:02
AFTER THIS → 2 DRILLS · PROBLEM #05
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
31 / DRILL UNIT 05 · Palindrome — Reverse the Half

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

The O(1)-space palindrome check reuses three earlier operations. Name them in order.

Middle → reverse half → compare. Locate the midpoint with fast/slow, reverse the nodes from the middle to the end in place, then run one pointer from the head and one from the new (reversed) second-half head, comparing values until they cross. Every step is a primitive from earlier units; the art is only in sequencing them and handling the odd-length middle node (which can be skipped). This composition mindset is what turns the deck-3 hards from scary into routine.

DRILL 02 · TRANSFER

An easier palindrome solution pushes all values onto a stack (or into a vector) and compares. Why might an interviewer reject it?

Correct, but O(n) space when O(1) is required. Copying the list into a stack or array and mirror-comparing is the obvious approach and a fine first answer — but the standard follow-up is “now do it in O(1) space,” which forces the reverse-the-half technique. Recognising that the space follow-up is really asking “can you compose middle + reverse?” is the signal this unit trains.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
32 / MECHANISM UNIT 05 · PALINDROME · CODE MIRRORED

MIDDLE, REVERSE THE HALF, COMPARE ENDS

The O(1)-space palindrome check chains three primitives you already know. Find the middle with fast/slow, reverse the second half in place, then walk one pointer from the front and one from the reversed half, comparing values. All matches ⇒ palindrome. It is the clearest demonstration that the medium problems are just the fundamentals composed — and politely, you restore the list by reversing the half back.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
33 / PROBLEM #05 · REVERSE · EASY

Palindrome Linked List

EASY reverse ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

“Is the list a palindrome”, ideally in O(1) space. The follow-up that forbids an array is really asking you to compose middle + reverse + compare.

INTUITION

Find the middle with fast/slow, reverse the second half in place, then walk one pointer from the head and one from the reversed half comparing values; any mismatch means not a palindrome. Optionally reverse the half back to restore the list.

STEPS
  1. Find the middle with fast/slow
  2. Reverse the list from the middle to the end
  3. p = head, q = reversed second half
  4. While q: if p->val != q->val return false; advance both
  5. Return true (optionally reverse the half back first)
BRUTEO(n) space (array/stack)
OPTIMALO(n)
↕ SCROLL
// Compose three primitives: middle, reverse half, compare ends.
bool isPalindrome(ListNode* head) {
    ListNode *slow = head, *fast = head;
    while (fast && fast->next) {            // 1) find the middle
        slow = slow->next;
        fast = fast->next->next;
    }
    ListNode *prev = nullptr;              // 2) reverse the second half
    while (slow) {
        ListNode* next = slow->next;
        slow->next = prev; prev = slow; slow = next;
    }
    ListNode *p = head, *q = prev;         // 3) compare from both ends
    while (q) {
        if (p->val != q->val) return false;
        p = p->next; q = q->next;
    }
    return true;
}
TIMEO(n)middle + reverse + compare, each linear
SPACEO(1)in-place reversal, no copy
TRAP

The O(n)-space stack is a first answer, not the final one. Pushing all values and mirror-comparing is correct but uses O(n) memory; the expected follow-up needs O(1), which forces the reverse-the-half approach. Handle the odd-length middle node (it can be skipped) and, if the list must survive, reverse the half back.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #05 · PALINDROME LINKED LIST

L10. Check if a LinkedList is Palindrome or Not | Multiple Approaches

The walkthrough for #05 Palindrome Linked List. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L10. Check if a LinkedList is Palindrome or Not | Multiple Approaches
RUNTIME 20:02
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
34 / INTRO UNIT 06 · Remove Nth — the Gap Trick

UNIT 06 — Remove Nth — the Gap Trick

You cannot count from the end of a singly list, so manufacture a gap. Advance fast n nodes ahead of slow, then move both together; when fast hits the last node, the preserved gap of n leaves slow exactly one before the target. A dummy head makes removing the first node just another case.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU REMOVE THE n-th NODE FROM THE END IN A SINGLE PASS?

fixed gaptwo pointersone passdummy headoff-by-one
WHAT TO WATCH FOR
  • 01ADVANCE fast n NODES FIRST — THAT FIXED GAP IS THE WHOLE TRICK
  • 02THEN MOVE fast AND slow TOGETHER UNTIL fast IS AT THE LAST NODE
  • 03slow IS NOW JUST BEFORE THE TARGET: slow->next = slow->next->next
  • 04USE A DUMMY head — THE TARGET MIGHT BE THE FIRST NODE
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
35 / VIDEO UNIT 06 · Remove Nth — the Gap Trick

L8. Remove Nth Node from the end of the LinkedList | Multiple Approaches

STRIVER A2Z
Remove Nth — the Gap Trick
RUNTIME 16:23
AFTER THIS → 2 DRILLS · PROBLEM #06
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
36 / DRILL UNIT 06 · Remove Nth — the Gap Trick

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

Why start slow at a dummy node (before the head) rather than at the head itself?

So the head is not a special case. If the node to delete is the very first one (n equals the length), you still need a prev to run prev->next = prev->next->next; the dummy is that predecessor. Anchoring slow at the dummy and advancing fast n + 1 from it leaves slow one before the target in every case, head included. Then return dummy->next as the possibly-new head.

DRILL 02 · TRACE

On 1 → 2 → 3 → 4 → 5 with n = 2, which node is removed, and where does slow stop?

Node 4 is removed; slow stops at 3. The 2nd node from the end of 1→2→3→4→5 is 4. Advance fast 2 steps to node 3 (value 3), then move both until fast reaches the last node (5): slow travels 1→2→3 and lands on 3, exactly one before 4. slow->next = slow->next->next bypasses 4, giving 1→2→3→5. The MECHANISM slide opens the gap first, then walks both pointers in lockstep.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
37 / MECHANISM UNIT 06 · REMOVENTH · CODE MIRRORED

A FIXED GAP TURNS 'FROM THE END' INTO 'FROM THE START'

You cannot index from the end of a singly list, so manufacture the offset: move fast n nodes ahead, then advance fast and slow together. When fast reaches the last node, the preserved gap of n puts slow exactly one before the target — bypass it. A dummy head handles the case where the node to remove is the head itself, which is the edge case this problem is really testing.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
38 / PROBLEM #06 · GAP · MED

Remove Nth Node From End of List

MED gap ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

“Remove the n-th node from the end” in one pass. You cannot index from the end of a singly list, so build a fixed-gap two-pointer window behind a dummy head.

INTUITION

Put a dummy before the head. Advance fast n + 1 nodes from the dummy, then move fast and slow together until fast is null; slow now sits just before the target. Bypass it and return dummy->next.

STEPS
  1. dummy->next = head; slow = fast = dummy
  2. Advance fast n + 1 steps (opens the gap and covers the head case)
  3. While fast: fast = fast->next; slow = slow->next
  4. slow->next = slow->next->next (unlink the target)
  5. Return dummy->next
BRUTEO(n) + O(n) (two passes)
OPTIMALO(n)
↕ SCROLL
// A gap of n turns 'from the end' into 'from the start'. Dummy handles head.
ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode dummy(0); dummy.next = head;
    ListNode* fast = &dummy;
    ListNode* slow = &dummy;
    for (int i = 0; i <= n; i++) fast = fast->next;   // gap of n (+1 from dummy)
    while (fast) { fast = fast->next; slow = slow->next; }
    slow->next = slow->next->next;                    // unlink the target
    return dummy.next;
}
TIMEO(n)a single pass with a fixed gap
SPACEO(1)two pointers plus a dummy
TRAP

Off-by-one on the gap, and the head-removal case. Advancing fast exactly n + 1 from the dummy leaves slow one before the target; n steps stops one short. The dummy is not optional: when n equals the length, the node to remove is the head, and only the dummy gives it a predecessor to splice against.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #06 · REMOVE NTH NODE FROM END OF LIST

L8. Remove Nth Node from the end of the LinkedList | Multiple Approaches

The walkthrough for #06 Remove Nth Node From End of List. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L8. Remove Nth Node from the end of the LinkedList | Multiple Approaches
RUNTIME 16:23
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
39 / INTRO UNIT 07 · Delete the Middle Node

UNIT 07 — Delete the Middle Node

Deleting the middle node is finding the middle with one addition: to unlink the node slow lands on, you need the node before it. A singly node cannot reach its own predecessor, so carry prev alongside slow as they walk. At the middle, prev->next = slow->next drops it — the fast/slow engine plus one trailing pointer.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU DELETE THE MIDDLE WHEN YOU CAN'T LOOK BACKWARD FROM IT?

fast/slow + prevtrailing pointerunlinkno back-pointerone-node edge
WHAT TO WATCH FOR
  • 01SAME FAST/SLOW WALK THAT FINDS THE MIDDLE
  • 02CARRY prev ONE NODE BEHIND slow — A SINGLY NODE HAS NO BACK-POINTER
  • 03AT THE MIDDLE: prev->next = slow->next UNLINKS IT
  • 04EDGE CASE: A ONE-NODE LIST DELETES TO EMPTY — RETURN null
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
40 / VIDEO UNIT 07 · Delete the Middle Node

L16. Delete the middle node of the LinkedList

STRIVER A2Z
Delete the Middle Node
RUNTIME 16:36
AFTER THIS → 2 DRILLS · PROBLEM #07
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
41 / DRILL UNIT 07 · Delete the Middle Node

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

Why must you track prev to delete the middle, when you already have slow sitting on it?

You delete a node through its predecessor. Removing slow means making the node before it skip over it: prev->next = slow->next. But a singly linked node has no prev field, so if you only kept slow you would have to re-walk from the head to find its predecessor. Carrying prev one hop behind slow during the same fast/slow pass gives it to you for free. (A doubly list, by contrast, could delete in O(1) with no prev variable — that is deck 1's lesson paying off.)

DRILL 02 · TRACE

On 1 → 2 → 3 → 4 → 5, which node does delete-middle remove?

Node 3. Fast/slow leaves slow on the middle node (value 3) with prev on node 2. Setting prev->next = slow->next links 2 straight to 4, producing 1 → 2 → 4 → 5. For an even list LeetCode deletes the second of the two middles, which the same guard produces. Watch the MECHANISM slide carry prev a step behind slow and strike the middle when it is reached.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
42 / MECHANISM UNIT 07 · DELETEMID · CODE MIRRORED

FAST/SLOW TO THE MIDDLE — BUT KEEP prev

Deleting the middle is finding the middle plus one wrinkle: to unlink the node slow lands on, you need the node before it. A singly node cannot reach its own predecessor, so carry prev alongside slow as they walk. When slow reaches the middle, prev->next = slow->next drops it. Same fast/slow engine as the middle problem, one extra pointer.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
43 / PROBLEM #07 · FAST-SLOW · MED

Delete the Middle Node of a Linked List

MED fast-slow ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

“Delete the middle node”. Fast/slow to the midpoint, but you must keep the predecessor to unlink it — a singly node can't look backward.

INTUITION

Walk fast (2) and slow (1) while carrying prev one node behind slow. When slow reaches the middle, prev->next = slow->next drops it. Guard the single-node list (delete to empty) — a dummy head makes this uniform.

STEPS
  1. If head or head->next is null, return null (0- or 1-node)
  2. slow = fast = head, prev = null
  3. While fast and fast->next: prev = slow; slow = slow->next; fast = fast->next->next
  4. prev->next = slow->next (unlink the middle)
  5. Return head
BRUTEO(n) + O(n)
OPTIMALO(n)
↕ SCROLL
// Fast/slow finds the middle; prev trails so you can unlink it.
ListNode* deleteMiddle(ListNode* head) {
    if (!head || !head->next) return nullptr;   // 1-node deletes to empty
    ListNode* slow = head;
    ListNode* fast = head;
    ListNode* prev = nullptr;
    while (fast && fast->next) {
        fast = fast->next->next;    // +2
        prev = slow;                // remember predecessor
        slow = slow->next;          // +1
    }
    prev->next = slow->next;        // unlink the middle
    return head;
}
TIMEO(n)one fast/slow pass
SPACEO(1)fast/slow plus a trailing prev
TRAP

Forgetting the single-node case, or dropping prev. A one-node list must return null (there is a middle to delete and nothing remains); without the guard you dereference a null prev. And you genuinely need prev — a singly node has no back-pointer, so you cannot unlink slow from slow alone.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #07 · DELETE THE MIDDLE NODE OF A LINKED LIST

L16. Delete the middle node of the LinkedList

The walkthrough for #07 Delete the Middle Node of a Linked List. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L16. Delete the middle node of the LinkedList
RUNTIME 16:36
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
44 / INTRO UNIT 08 · Odd / Even Rearrange

UNIT 08 — Odd / Even Rearrange

Group nodes by the parity of their position — 1st, 3rd, 5th, then 2nd, 4th — without moving any data. Keep an odd pointer, an even pointer, and a saved evenHead; each round, odd skips to the next odd node and even to the next even one, splitting the list into two interleaved chains. Finally hook odd->next = evenHead.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU REGROUP BY POSITION IN O(1) SPACE, KEEPING RELATIVE ORDER?

position parityweaveevenHead savedstitchorder preserved
WHAT TO WATCH FOR
  • 01odd = head, even = head->next, evenHead = even (SAVE IT!)
  • 02EACH ROUND: odd->next = even->next; odd = odd->next; THEN THE SAME FOR even
  • 03FINALLY: odd->next = evenHead — JOIN THE ODD CHAIN TO THE EVEN CHAIN
  • 04IT IS POSITION PARITY, NOT VALUE PARITY — ORDER IS PRESERVED IN EACH GROUP
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
45 / VIDEO UNIT 08 · Odd / Even Rearrange

L6. Odd Even Linked List | Multiple Approaches

STRIVER A2Z
Odd / Even Rearrange
RUNTIME 24:05
AFTER THIS → 2 DRILLS · PROBLEM #08
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
46 / DRILL UNIT 08 · Odd / Even Rearrange

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

Why save evenHead at the very start of the odd-even rearrange?

You need the even chain's head to stitch the two groups. The final step joins the tail of the odd chain to the front of the even chain: odd->next = evenHead. But as the weave proceeds, the pointers march forward and the original second node — the head of the even chain — would be lost if you had not stashed it. Saving evenHead before the loop is the small piece of bookkeeping the whole rearrange depends on.

DRILL 02 · TRACE

On 1 → 2 → 3 → 4 → 5, what does odd-even list produce?

1 → 3 → 5 → 2 → 4. The odd-position nodes are the 1st, 3rd, 5th (values 1, 3, 5) and the even-position nodes are the 2nd, 4th (values 2, 4); each group keeps its original relative order, and the odd chain is hooked to the even head. Note it is about position, not the values being odd or even — a list of 2 → 4 → 6 → 8 would still split by slot. The MECHANISM slide tints the odd-position nodes as the two chains form, then shows the final reorder.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
47 / MECHANISM UNIT 08 · ODDEVEN · CODE MIRRORED

WEAVE INTO TWO CHAINS, THEN STITCH

Group nodes by their position's parity — 1st, 3rd, 5th … then 2nd, 4th … — without moving data. Keep an odd pointer and an even pointer and a saved evenHead; each round, odd hops to the next odd node and even to the next even one, splitting the list into two interleaved chains. Finally odd->next = evenHead stitches them: all odd positions, then all even. O(1) space, order preserved within each group.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
48 / PROBLEM #08 · REARRANGE · MED

Odd Even Linked List

MED rearrange ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

“Group nodes at odd indices then even indices” (by position, not value), keeping relative order, in O(1) space. A weave-and-stitch rearrange.

INTUITION

Keep odd and even pointers and save evenHead. Each round, odd jumps to even->next (the next odd node) and even to odd->next (the next even node), splitting the list into two chains. Finally odd->next = evenHead joins them.

STEPS
  1. If head has fewer than 3 nodes, return head
  2. odd = head, even = head->next, evenHead = even
  3. While even and even->next:
  4. odd->next = even->next; odd = odd->next
  5. even->next = odd->next; even = even->next
  6. odd->next = evenHead; return head
BRUTEO(n) space
OPTIMALO(n)
↕ SCROLL
// Weave into an odd chain and an even chain, then stitch.
ListNode* oddEvenList(ListNode* head) {
    if (!head || !head->next) return head;
    ListNode* odd = head;
    ListNode* even = head->next;
    ListNode* evenHead = even;            // SAVE the even head
    while (even && even->next) {
        odd->next = even->next;  odd = odd->next;    // next odd
        even->next = odd->next;  even = even->next;   // next even
    }
    odd->next = evenHead;                 // stitch odd -> even
    return head;
}
TIMEO(n)one pass, constant pointer moves per node
SPACEO(1)in-place, no new nodes
TRAP

Not saving evenHead, and it's position not value. The final stitch needs the first even node, which is lost once the weave starts — stash it before the loop. And the grouping is by index (1st, 3rd, 5th…), not by whether the stored values are odd or even; misreading that solves a different problem.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #08 · ODD EVEN LINKED LIST

L6. Odd Even Linked List | Multiple Approaches

The walkthrough for #08 Odd Even Linked List. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L6. Odd Even Linked List | Multiple Approaches
RUNTIME 24:05
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
49 / INTRO UNIT 09 · Intersection of Two Lists

UNIT 09 — Intersection of Two Lists

Two lists of possibly different lengths that share a tail. Walk pA along A and pB along B; when either reaches null, redirect it to the other list's head. After one switch each pointer has travelled |A| + |B| nodes, so the length difference cancels and they land on the first shared node together — or both reach null if the lists never meet.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU FIND WHERE TWO LISTS MERGE, WITHOUT COUNTING THEIR LENGTHS?

switch headslength cancelsshared nodetwo pointers|A| + |B|
WHAT TO WATCH FOR
  • 01pA WALKS A THEN B; pB WALKS B THEN A — EACH COVERS |A| + |B|
  • 02THE SWITCH CANCELS THE LENGTH GAP, SO THEY ALIGN AT THE INTERSECTION
  • 03IF THEY NEVER INTERSECT, BOTH BECOME null AT THE SAME STEP (LOOP ENDS)
  • 04INTERSECTION IS A SHARED NODE (SAME ADDRESS), NOT A SHARED VALUE
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
50 / VIDEO UNIT 09 · Intersection of Two Lists

L12. Find the intersection point of Y LinkedList

STRIVER A2Z
Intersection of Two Lists
RUNTIME 32:05
AFTER THIS → 2 DRILLS · PROBLEM #09
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
51 / DRILL UNIT 09 · Intersection of Two Lists

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

Why does redirecting each pointer to the other list's head make them meet at the intersection?

Both travel the same total distance, so the gap disappears. Say A has length a before the shared tail and B has length b; a pointer that walks A then B covers a + (b + shared), and one that walks B then A covers b + (a + shared) — equal totals. So after each has switched once, they are the same number of steps from the end, and they arrive at the first shared node together. If there is no intersection, both hit null after a + b steps and the loop exits. It is the cleanest two-pointer trick in the deck.

DRILL 02 · RECALL

Two lists have nodes with equal values in the middle but are otherwise separate objects. Do they intersect?

No — intersection is about identity, not equality. Two lists intersect only if, from some node onward, they are the same physical nodes — the pointers converge and share a tail. Equal values along the way are irrelevant; comparing values would give false positives. The two-pointer method naturally checks identity because it compares the pointers themselves (pA == pB), which is why it returns the actual shared node. Reading “shared node” as address-identity is the conceptual trap here.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
52 / MECHANISM UNIT 09 · INTERSECTION · CODE MIRRORED

SWITCH HEADS TO CANCEL THE LENGTH DIFFERENCE

Two lists of different lengths that share a tail. Walk pA along A and pB along B; when either hits null, redirect it to the other list's head. After the switch each pointer has travelled |A| + |B| nodes, so the length gap cancels and they arrive at the first shared node together — or both reach null if the lists never meet. No length counting, O(1) space, and the neatest use of the two-pointer idea in the deck.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
53 / PROBLEM #09 · TWO-LIST · EASY

Intersection of Two Linked Lists

EASY two-list ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

“Find the node where two lists intersect” (share a tail). The O(1)-space trick is two pointers that switch heads to cancel the length difference.

INTUITION

Walk pA along A and pB along B; when a pointer hits null, redirect it to the other list's head. After one switch each has travelled |A| + |B| nodes, so they align and meet at the first shared node — or both become null together if the lists never intersect.

STEPS
  1. pA = headA, pB = headB
  2. While pA != pB:
  3. pA = pA ? pA->next : headB (switch at the end of A)
  4. pB = pB ? pB->next : headA (switch at the end of B)
  5. Return pA (the shared node, or null)
BRUTEO(n·m) or O(n) space
OPTIMALO(n + m)
↕ SCROLL
// Switch heads at each end: both walk |A|+|B|, so lengths cancel.
ListNode* getIntersectionNode(ListNode* a, ListNode* b) {
    ListNode* pA = a;
    ListNode* pB = b;
    while (pA != pB) {
        pA = pA ? pA->next : b;   // A exhausted -> head of B
        pB = pB ? pB->next : a;   // B exhausted -> head of A
    }
    return pA;                    // shared node, or null if none
}
TIMEO(n + m)each pointer walks both lists once
SPACEO(1)two pointers, no length count or set
TRAP

Comparing values, and the no-intersection exit. Intersection means the same node (identity), so compare pointers, not ->val. The loop is guaranteed to end: if the lists never meet, both pointers reach null after |A| + |B| steps and pA == pB == null, which correctly returns null — no infinite loop.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #09 · INTERSECTION OF TWO LINKED LISTS

L12. Find the intersection point of Y LinkedList

The walkthrough for #09 Intersection of Two Linked Lists. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L12. Find the intersection point of Y LinkedList
RUNTIME 32:05
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
54 / INTRO UNIT 10 · Add Two Numbers

UNIT 10 — Add Two Numbers

The digits are stored reversed, which is the gift: the ones place sits at each head, so you add node by node from the front exactly as you would on paper. Carry a carry, append sum % 10 to a dummy-headed result list, and continue while either input remains or a carry is left over. Return dummy.next.

THE QUESTION THIS LECTURE ANSWERS

HOW DO YOU ADD TWO NUMBERS STORED AS LINKED DIGITS, LEAST-SIGNIFICANT FIRST?

reversed digitscarrydummy headsum % 10trailing carry
WHAT TO WATCH FOR
  • 01REVERSED STORAGE MEANS THE ONES ALIGN AT THE HEADS — ADD FRONT TO BACK
  • 02sum = l1 + l2 + carry; DIGIT = sum % 10; carry = sum / 10
  • 03LOOP while (l1 || l2 || carry) — A MISSING NODE COUNTS AS 0
  • 04THE FINAL carry (e.g. 99 + 1) NEEDS ITS OWN NODE — DO NOT FORGET IT
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
55 / VIDEO UNIT 10 · Add Two Numbers

L5. Add 2 numbers in LinkedList | Dummy Node Approach

STRIVER A2Z
Add Two Numbers
RUNTIME 14:48
AFTER THIS → 2 DRILLS · PROBLEM #10
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
56 / DRILL UNIT 10 · Add Two Numbers

CHECK — CAN YOU ANSWER IT WITHOUT LOOKING BACK?

DRILL 01 · RECALL

Why is the loop condition while (l1 || l2 || carry) instead of while (l1 && l2)?

Unequal lengths and a leftover carry. Stopping when the shorter list ends (&&) truncates the sum; and even after both lists are exhausted, a carry of 1 (as in 99 + 1 = 100) still needs a final node. Looping while any of l1, l2, or carry is non-zero — and treating a missing node as contributing 0 — handles both. It is the most common correctness bug in this problem.

DRILL 02 · TRACE

Adding 2 → 4 → 3 and 5 → 6 → 4 (i.e. 342 + 465), what is the result list?

7 → 0 → 8. Add head to head with carry: ones 2+5 = 7 (carry 0); tens 4+6 = 10 → digit 0, carry 1; hundreds 3+4+1 = 8 (carry 0). The result list, still least-significant-first, is 7 → 0 → 8, i.e. the number 807 = 342 + 465. No trailing carry here, but flip one input to make it 999 and you would get the extra node. The MECHANISM slide shows the running carry and the result list growing behind the dummy.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
57 / MECHANISM UNIT 10 · ADDTWO · CODE MIRRORED

ONES ALIGN AT THE HEAD — ADD WITH A CARRY

The digits are stored reversed, which is the gift: the ones place is at each head, so you add node by node, front to back, exactly as you would on paper. Carry a carry, append sum % 10 to a dummy-headed result list, and keep going while either input remains or a carry is left over — that final carry (think 99 + 1) is the case people forget. The dummy makes appending uniform from the very first digit.

RE-ROUTING THE ARROWS
CODE MIRROR
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
58 / PROBLEM #10 · TWO-LIST · MED

Add Two Numbers

MED two-list ▶ SOLVE ON LEETCODE
SIGNAL — WHAT GIVES IT AWAY

Add two numbers given as linked lists, one digit per node, least-significant digit first”. Reversed storage lets you add front to back with a carry, building the result behind a dummy.

INTUITION

Walk both lists together. At each step sum = l1 + l2 + carry; append sum % 10 to a dummy-headed result and set carry = sum / 10. Continue while either list has nodes or a carry remains, treating a missing node as 0.

STEPS
  1. dummy; cur = &dummy; carry = 0
  2. While l1 or l2 or carry:
  3. sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry
  4. carry = sum / 10; append new node with sum % 10
  5. advance l1 and l2 if present; return dummy.next
BRUTEO(max(n,m))
OPTIMALO(n + m)
↕ SCROLL
// Digits are reversed, so add head-to-head with a carry, behind a dummy.
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode dummy(0);
    ListNode* cur = &dummy;
    int carry = 0;
    while (l1 || l2 || carry) {
        int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry;
        carry = sum / 10;
        cur->next = new ListNode(sum % 10);   // append the digit
        cur = cur->next;
        if (l1) l1 = l1->next;
        if (l2) l2 = l2->next;
    }
    return dummy.next;
}
TIMEO(n + m)one pass over the longer list
SPACEO(1)O(1) extra beyond the result list itself
TRAP

Dropping the final carry, or stopping at the shorter list. Loop while l1 || l2 || carry and treat a missing node as 0: ending when the shorter list runs out truncates the sum, and a leftover carry (99 + 1 = 100) needs its own final node. The dummy head makes appending the first digit uniform.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
SOLUTION #10 · ADD TWO NUMBERS

L5. Add 2 numbers in LinkedList | Dummy Node Approach

The walkthrough for #10 Add Two Numbers. Watch it, then go straight back and write it yourself.

SOLUTION WALKTHROUGH
L5. Add 2 numbers in LinkedList | Dummy Node Approach
RUNTIME 14:48
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
59 / RECALL RETRIEVAL, NOT RECOGNITION

NAME THE TRICK FROM THE STATEMENT

DRILL 01 · TRANSFER

Palindrome-linked-list in O(1) space is built entirely from three earlier primitives. Which three?

Middle, reverse, compare. There is no new idea in the O(1) palindrome check — it is fast/slow to locate the midpoint, an in-place reversal of the back half, and a two-pointer comparison walking inward from both ends. Recognising a medium problem as a composition of fundamentals is the entire game of this deck: once the three primitives are automatic, the “hard” version is just deciding the order to run them in (and, politely, reversing the half back to leave the list intact).

DRILL 02 · RECALL

Remove-nth-from-end and add-two-numbers both open with ListNode dummy; dummy.next = head;. What problem does the dummy head solve in each?

It removes the “the head might change” special case. In remove-nth, the target can be the head itself, so without a dummy you would branch to reassign head; the dummy gives position 0 a real predecessor. In add-two, the dummy is the anchor you append the first result digit to, so building the list is uniform from the start and you simply return dummy.next. Any time an operation can touch the first node, reach for the dummy — it is the single most reused trick across decks 1–3.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
60 / TRAPS PLAUSIBLE, WRONG, AND SILENT

SIX WAYS TO BE PLAUSIBLY WRONG

Pointer bugs compile and pass the sample: a missing null guard, a flip before the save, a phase-2 step size, a dropped carry. Every one returns a believable wrong answer.

FAST/SLOW: THE NULL GUARD AND WHICH MIDDLE

The loop must be while (fast && fast->next) — drop either check and the double step dereferences null on an even-length list. And that exact guard decides whether slow lands on the first or second middle; problems like delete-middle care which.

REVERSE: LOSING THE LIST, OR RETURNING THE OLD head

Save next before flipping curr->next, or the rest of the list is unreachable. And return prev — it walked to the old tail, the new head; returning the old head gives you a one-node list.

FLOYD: WHERE THE POINTERS START

Start both at head and advance inside the loop (slow = slow->next; fast = fast->next->next then compare). Starting fast at head->next is a different convention that changes the meeting math — pick one and keep it consistent.

CYCLE II: PHASE-2 STEP SIZE

After the meeting, both pointers move one step at a time — not two. Reset one to head, keep the other at the meeting point, and single-step both. Moving fast by two in phase 2 overshoots the loop's start.

REMOVE Nth: OFF-BY-ONE AND THE HEAD CASE

The gap must leave slow before the target, so advance fast the right number of steps (with a dummy, n + 1 from the dummy). Removing the head itself (n == length) is why the dummy head is not optional here.

ADD TWO NUMBERS: THE FINAL CARRY AND UNEQUAL LENGTHS

Loop while (l1 || l2 || carry), treating a missing node as 0. Forgetting the trailing carry drops the leading 1 on cases like 99 + 1; stopping when the shorter list ends truncates the answer.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
61 / CHEATSHEET REVISION SURFACE

ONE SCREEN, THE WHOLE DECK

Ten problems, ten one-liners. The right-hand column is the move — the pointer choreography that should come to mind the instant you read the statement.

ALGORITHM
TIME
SPACE
REACH FOR IT WHEN
Middle (fast/slow)
O(n)
O(1)
slow +1, fast +2 — slow lands at the middle
Reverse (3-pointer)
O(n)
O(1)
save next, flip curr->next = prev, slide
Cycle detect (Floyd)
O(n)
O(1)
hare laps tortoise ⇒ they meet ⇒ cycle
Cycle start
O(n)
O(1)
after meeting, reset one to head, step both by 1
Palindrome
O(n)
O(1)
middle → reverse 2nd half → compare ends
Remove nth from end
O(n)
O(1)
gap of n, move both, dummy for the head case
Delete the middle
O(n)
O(1)
fast/slow + prev, then prev->next = slow->next
Odd / even
O(n)
O(1)
weave two chains, odd->next = evenHead
Intersection
O(n+m)
O(1)
switch heads at null — lengths cancel
Add two numbers
O(n+m)
O(1)
dummy + carry, loop while l1|l2|carry
INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
62 / CLOSE STEP 06 · DECK 2 OF 3

TEN MEDIUMS, SIX TRICKS

Fast/slow, reverse, Floyd, the gap, the rearrange, the tandem walk — that is the whole vocabulary. The hards in deck 3 (reverse-in-k-group, sort, LRU, merge-k, copy-random) are these six composed, so once they are automatic the last deck is bookkeeping.

00%
OF THIS DECK SOLVED
← ALL TOPICS← DECK 1 · FUNDAMENTALSDECK 3 · HARD →

Lectures are Striver's A2Z linked-list playlist (L5, L6, L8–L10, L12–L14, L16, L17). Problems are LeetCode; the hard problems are deck 3.

INVARIANT · LINKED LIST · THE POINTER TRICKS THAT SOLVE THE MEDIUMS · DECK 2 OF 3
■ TOO SMALL TO READ
1280×720
INVARIANT · STEP 06 · DECK 2 OF 3

This one needs a laptop

Not a preference — an arithmetic one. Every slide is a fixed 1280 × 720 stage: a graph animating beside the code that drives it, with the problems laid out two columns wide. It scales as a single piece, so on a screen this size the body text comes out around 4px tall.

Shrinking it further would not help, and rebuilding it to reflow would mean losing the thing that makes it worth reading.

YOUR SCREEN0 × 0
NEEDED1060 × 610
WHAT IS WAITING ON THE LAPTOP
WATCH IT RUN, THEN RUN IT FROM MEMORY
Your progress is saved per device, so anything you tick on the laptop will be waiting there.