DSA Guide
Linked Lists

Linked Lists

Pointer rewiring, fast and slow pointers, and the dummy-head trick

A linked list trades random access for cheap insertion. You cannot jump to index k — you must follow k pointers — but splicing a node in or out is O(1) once you are holding the right reference. Every technique below exists because of that trade.

The Three Techniques

1. Careful pointer rewiring

The defining hazard: overwriting a next pointer destroys your only reference to the rest of the list. Always save before you overwrite.

2. Fast and slow pointers

Two pointers moving at different rates turn several questions into a single pass.

3. The dummy head

Create a throwaway node in front of the result. Every append becomes uniform, and the "what about the first node?" special case disappears.

Two habits that prevent most linked-list bugs

Check fast before fast.next. Both languages short-circuit left to right, so while fast and fast.next never dereferences null. Reversing the order crashes.

Use a dummy head whenever the head might change. Deletions at the front, merges, and insertions all get simpler.

Node Definitions

from typing import Optional


class ListNode:
    def __init__(self, val: int = 0, next: Optional["ListNode"] = None):
        self.val = val
        self.next = next

Identity vs. Value

A recurring source of wrong answers: two nodes holding the same number are not the same node. When a problem asks where two lists intersect, or whether a list revisits a node, compare with is in Python and === in TypeScript — never .val.

All Problems

ProblemDifficultyPattern
Reverse Linked ListEasyPointer Rewiring
Middle of the Linked ListEasyFast & Slow
Merge Two Sorted ListsEasyDummy Head
Intersection of Two Linked ListsEasyTwo Pointers
Linked List Cycle IIMediumFloyd's Algorithm

Suggested order

Reverse Linked List first — it teaches the pointer discipline everything else depends on. Then Middle of the Linked List, which introduces fast and slow pointers gently before Linked List Cycle II pushes them much further.

On this page