Linked lists and pointer manipulation
Start with no coding-interview background, learn to trace an algorithm by hand, measure its cost, and finish able to reason about arrays, hashing, trees, graphs, dynamic programming and the trade-offs an interviewer expects you to name.
What you will be able to do
#- Traverse a singly linked list and reverse it with three pointers.
- Use a dummy head to simplify insertion and deletion.
- Detect a cycle with fast and slow pointers.
Overview
#Nodes and links
A linked list is a chain of nodes, each holding a value and a reference to the next node. You cannot jump to index i directly, so access is O(n), but inserting or deleting a node you already hold is O(1) because only neighbouring links change.
class Node {
constructor(value) { this.value = value; this.next = null; }
}
Reversal with three pointers
function reverse(head) {
let prev = null;
let curr = head;
while (curr) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
Save next before overwriting curr.next, then advance. After the loop, prev is the new head.
Dummy head
When a change might affect the first node, create a placeholder node whose next is the real head. Then every insertion and deletion has a previous node to work with, and you return dummy.next at the end. This removes the special case for the head.
Cycle detection
Floyd's algorithm uses a slow pointer that moves one step and a fast pointer that moves two. If there is a cycle, the fast pointer laps the slow one and they meet. If the fast pointer reaches the end, there is no cycle. This is O(n) time and O(1) space, unlike a visited-set approach which costs O(n) space.
Practice next
- Merge two sorted linked lists using a dummy head.
- Find the middle of a list with slow and fast pointers.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
Reversing the list 1 -> 2 -> 3 -> null yields what sequence?
starter.javascript// prev=null, curr=1, then relink each nodeReveal the reference answer
reference.javascriptThe links reverse: 3 -> 2 -> 1 -> null. The new head is the node holding 3.Authored expected output: 3 -> 2 -> 1 -> null
Each node points to the previous node after the loop.
Exercise 2Debug
This reversal loses the rest of the list. Find the bug.
starter.javascriptfunction reverse(head) { let prev = null; let curr = head; while (curr) { curr.next = prev; prev = curr; curr = curr.next; } return prev; }Reveal the reference answer
reference.javascriptfunction reverse(head) { let prev = null; let curr = head; while (curr) { const next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; }Authored expected output: Fully reversed list
The buggy version overwrites curr.next before saving it, so curr becomes null and the loop stops after one node.
Exercise 3Explain
Explain why Floyd's slow and fast pointers meet inside a cycle.
Reveal the reference answer
reference.textOnce both pointers enter the cycle, the fast pointer gains one position on the slow pointer per step. Because the cycle is finite, that gap must eventually become a multiple of the cycle length, at which point they occupy the same node. If the list has no cycle, the fast pointer reaches null and the loop ends.Authored expected output: The fast pointer gains one step per iteration inside a finite cycle until they coincide.
This is why cycle detection needs no extra memory.
Common misconceptions
#- Linked list access is O(1) like an array. It is O(n) because you must follow links from the head.
- You can reverse a list without saving next. Overwriting curr.next first loses the rest of the list.
- Cycle detection needs a hash set. Floyd's two-pointer method needs no extra space.
Follow-up practice
#- dsa-from-zero-recursion
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.