Skip to content
DSAPrep

    ↑↓ to move · Enter to open · Esc to close

    ← All patterns

    Linked List

    Pointer surgery: dummy heads, fast/slow pointers, in-place reversal.

    Reach for it when

    • Reverse, merge, reorder, or remove nodes
    • Detect a cycle or find the middle
    • O(1) extra space required

    Core idea

    Draw the pointers before changing them. A dummy node before the head removes the special case of editing the head. Fast/slow pointers find the middle (fast moves two steps) and detect cycles (they meet inside a loop).

    Template · O(n) time, O(1) space

    def reverse(head):
    prev, cur = None, head
    while cur:
    nxt = cur.next # save before overwriting
    cur.next = prev
    prev, cur = cur, nxt
    return prev

    Common pitfalls

    • Losing the rest of the list by overwriting next before saving it
    • Null checks on fast and fast.next
    • Returning head instead of dummy.next after edits

    Practice · 0 / 11 done