Singly linked lists: traverse, insert, delete
A singly linked list is a chain of nodes, each pointing to the next. Walk the chain with a cursor and rewire two arrows to insert or delete.
The idea
In a treasure hunt each clue tells you where the next clue is. You cannot jump to the last clue.
A linked list stores data the same way. Each item knows only where the next item is, so you find things by following the chain.
Intuition
Each box is a node: a value and an arrow to the next node. head names the first node.
Nodes can live anywhere in memory. The next arrows, not the positions, decide the order.
There is no index to jump with. To reach 30, cur starts at head and follows two arrows.
Adding 25 after 7 changes two arrows: 7 → 25 and 25 → 30. No value shifts, unlike in an array.
Complexity
Reaching index k takes k hops in a list but one jump in an array. Search is O(n) for the same reason.
An array computes the address of index k. A list only knows the head, so it must walk every node before k.
Once cur sits on the right node, insert and delete are O(1). Only two arrows change.
The walk to find the node is the expensive part. Inserting at the head needs no walk at all, so it is O(1) overall.
Pitfalls
Set cur.next first and new.next copies the new node itself: 99 points to 99. Node 1 is lost for good.
After cur.next = new, the old next is no longer stored anywhere. Always copy cur.next into new.next before changing cur.next.
Variations
A doubly linked list adds a prev arrow to each node, so you can walk both ways.
In a circular list the tail points back to the head instead of to null.
A dummy node D before the head makes deleting the head the same as any other delete. prev starts on D.
Code
Pseudocode
if head = null: return null
if head.value = target: return head.next
cur ← head
while cur.next ≠ null
if cur.next.value = target
cur.next ← cur.next.next
return head
cur ← cur.next
return head
Python
def delete_value(head, target):
if head is None:
return None
if head.val == target:
return head.next
cur = head
while cur.next is not None:
if cur.next.val == target:
cur.next = cur.next.next
return head
cur = cur.next
return head
JavaScript
function deleteValue(head, target) {
if (head === null) return null;
if (head.val === target) return head.next;
let cur = head;
while (cur.next !== null) {
if (cur.next.val === target) {
cur.next = cur.next.next;
return head;
}
cur = cur.next;
}
return head;
}
Java
static ListNode deleteValue(ListNode head, int target) {
if (head == null) return null;
if (head.val == target) return head.next;
ListNode cur = head;
while (cur.next != null) {
if (cur.next.val == target) {
cur.next = cur.next.next;
return head;
}
cur = cur.next;
}
return head;
}
Cost at a glance
| Access | O(n) |
|---|---|
| Search | O(n) |
| Insert | O(1) at head or after a node |
| Delete | O(1) after a node / O(n) by value |
| Space | O(n) |
Practice problems (6)
Insert after a node Warm-up
Insert 99 after the node holding 5 in 3 → 8 → 5 → 1. Answer each question before the step plays.
Length of a linked list Warm-up
Given the head of a singly linked list, return how many nodes it has. An empty list (head is null) has length 0.
Delete by value Standard
Given the head of a singly linked list and a value val, remove every node whose value equals val. Return the head of the list that remains (null when every node is removed).
Remove the nth node from the end Standard
Given the head of a singly linked list and an integer n, remove the node that is n places from the end (n = 1 is the tail) and return the head. Try to do it in one pass.
Spot the bug: arrows set in the wrong order Standard
insertAfter(head, after, value) should put a new node holding value right after the first node holding after. It loses part of the list. Click the line with the bug, then fix it.
Remove all duplicates from a sorted list Challenge
Given the head of a sorted singly linked list, delete every node whose value appears more than once, so only values that were unique remain. Keep the remaining nodes in order and return the head.
This removes all copies, not just the extra ones: [1, 1, 2] becomes [2].
Next on the path: Linked list techniques: reverse, middle, merge, detect a cycle