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
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
}
}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 endThe list 1 → 2 → 3:
The problems give you the ListNode class already; you just receive the head.
Array vs. linked list
| Operation | Array | Linked list |
|---|---|---|
Get the item at position i | O(1): arr[i] | O(n): walk i steps from the head |
| Insert/remove at the front | O(n): shift items | O(1): change one pointer |
| Insert/remove in the middle | O(n): shift items | O(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
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:
const next = current.next // bookmark
current.next = prev // now it's safe to rewire
current = next // continue from the bookmarkUsed 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:
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,
fasteventually catchesslow(Linked List Cycle). - When
fastreaches the end,slowis 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
nextwithout saving it. - Crashing on
null: accessingnode.next.nextwhennode.nextisnull. 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
===(orisin Python).