Traversal and dummy nodes
A linked list is a chain of nodes, each holding a value and a pointer to the next node. You can only walk it from the head, one node at a time. Most changes are "find the node before the spot, then rewire one or two pointers", and a dummy node placed before the head removes the special case of changing the head.
How to recognise it
- The input is a ListNode head (or several), and the answer is a list, a value, or a node.
- Nodes must be inserted, deleted, moved or regrouped without copying the values into an array.
- The head itself might be deleted or replaced.
The idea in one picture
A linked list is a treasure hunt: each clue (node) holds a value and tells you where the next clue is. The head is the
first clue, and the last one points to nothing (null).
| head → | 3 → | 4 → | 5 → | null |
|---|
You cannot jump to the middle. To reach node k, you follow k - 1 arrows. In exchange, adding or removing a node is
just rewiring arrows, with no shifting of other values.
The two moves everything is built from
| Move | Code | Picture |
|---|---|---|
Delete the node after prev | prev.next = prev.next.next | 3 → 4 → 5 becomes 3 → 5 |
Insert x after prev | x.next = prev.next; prev.next = x | 3 → 5 becomes 3 → x → 5 |
Both need the node before the spot. Finding that node is the walk; changing the arrows is O(1).
Template: walk with a dummy node
dummy = new node; dummy.next = head
prev = dummy
while prev.next is not null:
if prev.next is the node to change:
rewire around prev.next
(stop, or keep going without moving prev)
else:
prev = prev.next
return dummy.next
With the dummy, the head is just "the node after dummy", so deleting it or inserting before it is not special.
Building new lists from old nodes
Several problems split one list into chains (odd and even positions; 0s, 1s and 2s) and then join the chains. Give each
chain its own dummy head and a tail pointer, append nodes to the right tail as you walk, then link the tails together and
end the last chain with null.
Common mistakes
- Losing the rest of the list by overwriting
prev.nextbefore saving it. - Forgetting to end a rebuilt chain with
null, which can leave a cycle. - Moving
prevforward right after deleting, which skips a node. - Returning
headwhen the head itself was deleted. Returndummy.next. - Reading
node.next.valwithout checking thatnode.nextexists.
Problems
- Traversal in Linked Listeasyguide ready
- Deletion of the tail of Linked Listeasyguide ready
- Deletion of the Kth element of Linked Listeasyguide ready
- Delete the element with value Xeasyguide ready
- Insertion at the head of Linked Listeasyguide ready
- Insertion at the tail of Linked Listeasyguide ready
- Insertion at the Kth position of Linked Listeasyguide ready
- Insertion before the value X in Linked Listeasyguide ready
- Sort a Linked List of 0's 1's and 2'seasyguide ready
- Intersection of Two Linked Listseasyguide ready
- Remove Duplicates in a sorted Linked Listeasyguide ready
- Remove Duplicates in a Un-sorted Linked Listeasyguide ready
- Write a Program to Move the last element to Front in a Linked Listeasyguide ready
- Multiply 2 no. represented by LLeasyguide ready
- Delete Node in a Linked Listmediumguide ready
- Add Two Numbersmediumguide ready
- Odd Even Linked Listmediumguide ready
- Rotate Listmediumguide ready