Skip to content
Guest
Learn DSA · Course Syllabus Show lessons
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:

  1. In-place reversal: Save next = curr.next, flip curr.next = prev, advance prev = curr; curr = next.
  2. Fast & Slow pointers (Floyd’s Tortoise & Hare): Move slow by 1 step and fast by 2 steps. When fast reaches the end, slow is at the exact middle; if there is a cycle, fast catches up to slow in $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.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind