The pattern: linked lists
Master pointers, sentinel dummy nodes, fast and slow runners, and in-place link rewiring to solve linked list interview problems without extra memory.
A singly linked list is a sequence of node objects where each node holds a val and a reference to the next node. Unlike arrays, nodes are not stored contiguously in memory: there is no random access by index (O(n) to reach the k-th node), but inserting or deleting a node takes O(1) time once you hold a reference to the preceding node.
Most interview questions test your ability to reorder, inspect, or rewire nodes in place using O(1) auxiliary space without losing references.
01The core techniques
1. Sentinel / Dummy Node
When deleting or inserting nodes, the head node of the list might change or be removed entirely. Managing special-case code for the head adds error-prone branches.
A dummy head sits right before the real list:
dummy = ListNode(0)
dummy.next = head
curr = dummyNow every real node—including the original head—has a node preceding it. Deletions and insertions use identical logic everywhere, and the result is always returned as dummy.next.
Used for: Remove Linked List Elements, Merge Two Sorted Lists, Remove N-th Node From End, Reverse Linked List II.
2. Fast & Slow Pointers (Tortoise and Hare)
Two pointers move through the list at different speeds:
slowadvances 1 step at a time:slow = slow.next.fastadvances 2 steps at a time:fast = fast.next.next.
slow = head ; fast = head
while fast != null and fast.next != null:
slow = slow.next
fast = fast.next.nextTwo classic applications:
- Finding the middle node: When
fastreaches the end,slowis exactly halfway through the list. - Cycle detection (Floyd's algorithm): If a cycle exists,
fastenters the loop and lapsslowfrom behind; they are guaranteed to meet. Iffasthitsnull, the list is linear and acyclic.
Used for: Middle of the Linked List, Linked List Cycle, Linked List Cycle II, Palindrome Linked List.
3. Pointer Reversal and In-Place Rewiring
To reverse links between nodes without allocating new node objects, track three pointers: prev, curr, and next:
prev = null ; curr = head
while curr != null:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
return prevAlways save curr.next into a temporary variable before overwriting curr.next = prev, otherwise you orphan the remainder of the list.
Used for: Palindrome Linked List (reverse second half), Reverse Linked List II.
4. Gap / Two-Pointer Offset
Maintain a fixed distance of k steps between a leading pointer and a trailing pointer. Advance the leader k steps forward first, then step both forward together until the leader hits the tail.
Used for: Remove N-th Node From End of List, Intersection of Two Linked Lists (virtual alignment by walking both lists).
02How to spot it
- The problem provides a
ListNodehead and asks for traversal, cycle checking, or reordering. - The constraint says "in place" or "O(1) extra space", forbidding copies into arrays or
ArrayLists. - The operation involves finding a middle element, detecting an infinite loop, or splicing two sorted chains.
03The golden rule of linked list pointers
Before writing node.next.something, always ask: could node or node.next be null? Null pointer exceptions are the single most common bug in linked list interviews. Keep loop conditions guarded: while (fast != null && fast.next != null).