Core Data Structures · 9 min read · Intermediate
Linked Lists & Fast/Slow Pointers
Reverse a singly linked list in O(1) space and detect cycles or find the middle node using Floyd's Fast/Slow pointers.
What you'll learn
- Reverse a linked list iteratively with prev, curr, and next pointers
- Detect cycles in O(n) time and O(1) space using slow (+1) and fast (+2) pointers
- Know when linked lists win (O(1) head insert/delete) vs. arrays (cache locality & O(1) indexing)
Two classic pointer techniques solve 80% of linked list interview problems:
- In-place reversal: Save
next = curr.next, flipcurr.next = prev, advanceprev = curr; curr = next. - Fast & Slow pointers (Floyd’s Tortoise & Hare): Move
slowby 1 step andfastby 2 steps. Whenfastreaches the end,slowis at the exact middle; if there is a cycle,fastcatches up toslowin $O(1)$ space!
Fill-in-the-Blank Exercise
Reverse a Linked List
Save curr.next, reverse the link to prev, and advance both pointers.
while (curr) {
const next = curr.next;
curr.next = ;
prev = curr;
curr = ;
}Live DSA Playground
In-Place Linked List Reversal
Live Result
Console Output
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
Score: 0 / 1
1. How much extra memory does Floyd's Fast & Slow pointer cycle detection require?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.