Skip to main content
Back to all problems
Beginner guide, Algorithms & data structures

Linked List

Pointer manipulation: reversing, merging, and fast & slow pointers.

A linked list is a chain of small objects called nodes. Each node holds a value and a pointer to the next node. Unlike an array, the items aren't stored side by side in memory: you only get the first node (the head), and you reach the rest by following the pointers one by one.

What a node looks like

js
class ListNode {
  constructor(val = 0, next = null) {
    this.val = val // the value stored in this node
    this.next = next // the next node, or null at the end
  }
}
python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val    # the value stored in this node
        self.next = next  # the next node, or None at the end

The list 1 → 2 → 3:

Loading diagram...

The problems give you the ListNode class already; you just receive the head.

Array vs. linked list

OperationArrayLinked list
Get the item at position iO(1): arr[i]O(n): walk i steps from the head
Insert/remove at the frontO(n): shift itemsO(1): change one pointer
Insert/remove in the middleO(n): shift itemsO(1), if you already have the node before it

In interviews, linked list problems are mostly about one skill: rewiring pointers without losing anything.

Walking a list

js
let current = head
while (current) {
  // do something with current.val
  current = current.next
}

When current becomes null, you've passed the last node.

The four tricks you'll use

1. Save next before you change it

The moment you overwrite node.next, you lose your only link to the rest of the list. Always bookmark it first:

js
const next = current.next // bookmark
current.next = prev // now it's safe to rewire
current = next // continue from the bookmark

Used in: Reverse Linked List, Reorder List.

2. The dummy node

Building or editing a list often needs a special case for the head (it has no node before it). A fake node in front removes that case:

js
const dummy = new ListNode(0, head)
// ... every real node, including head, now has a node before it
return dummy.next // the real head (which may have changed)

Used in: Merge Two Sorted Lists, Remove Nth Node From End, Merge K Sorted Lists.

3. Fast and slow pointers

Two pointers walking at different speeds: slow moves 1 step, fast moves 2.

  • If there's a loop, fast eventually catches slow (Linked List Cycle).
  • When fast reaches the end, slow is at the middle (Reorder List).

4. A fixed gap

Two pointers at the same speed, but one starts n nodes ahead. When the front one reaches the end, the back one is n from the end (Remove Nth Node From End).

Draw it!

Linked list bugs are almost always "I lost a node" or "I made a loop". Before coding, draw boxes and arrows on paper, and redraw them after each pointer change. In an interview, this is expected, not a sign of weakness.

How to recognize this category

The problem gives you a head and ListNode. Then ask:

  • Do I need to reverse part of it? → trick 1
  • Might I remove or replace the head? → trick 2
  • Do I need the middle, or to detect a loop? → trick 3
  • Do I need a position counted from the end? → trick 4

Common mistakes

  • Losing the rest of the list by overwriting next without saving it.
  • Crashing on null: accessing node.next.next when node.next is null. Check before you step.
  • Returning the old head after the head changed. With a dummy node, return dummy.next.
  • Comparing values instead of nodes when asking "is this the same node?". Use === (or is in Python).

Practice what you learned

The problems go from easier to harder. Try each one on your own and open a hint when you get stuck.