Singly Linked Lists
Instead of one contiguous memory block, a linked list is a chain of nodes scattered anywhere in memory — each holding a value and a pointer to the next. The tradeoff: O(1) head insertion, O(n) index access.
What is a singly linked list?
A singly linked list is a chain of nodes where each node holds a value and a reference to the next node; the last node points to null. Adding or removing at the head is O(1), but finding the k-th item means walking from the head, which is O(n). Linked lists shine when you insert and remove often and rarely need random access.
class Node { constructor(value, next = null) { this.value = value; this.next = next; } }
let head = new Node(1, new Node(2, new Node(3))); // 1 → 2 → 3
head = new Node(0, head); // prepend O(1)
for (let n = head; n; n = n.next) console.log(n.value); // traverse O(n)
function reverse(head) { // classic interview question
let prev = null;
while (head) [head.next, prev, head] = [prev, head, head.next];
return prev;
}Node anatomy
Each node contains exactly two things: its data value, and a pointer (memory address) to the next node. The last node's pointer is null — the sentinel that signals the end.
0x100
42
next: 0x200
0x200
17
next: 0x350
0x350
93
next: 0x410
0x410
5
next: null
Live demo — try the operations
Insert at head is O(1) — just update one pointer. Insert at tail requires walking every node to find the end: O(n). Watch the pointer chain update with every operation.
Singly Linked List
length: 4Core operations
1Node: { value, next <- null }2List: { head <- null, length <- 0 }34prepend(value): // O(1)5 newNode <- Node(value)6 newNode.next <- head7 head <- newNode8 length += 1910append(value): // O(n) — must reach tail11 newNode <- Node(value)12 if head = null: head <- newNode; return13 current <- head14 while current.next != null: current <- current.next15 current.next <- newNode1617deleteHead(): // O(1)18 if head = null: error19 head <- head.next2021getAt(index): // O(n)22 current <- head23 for i from 0 to index-1: current <- current.next24 return current.value
Reversing a linked list
One of the most common interview problems. The key insight: traverse once, re-point each node's next pointer backwards. Three pointers: prev, current, next.
1reverse(list):2 prev <- null3 current <- head45 while current != null:6 nextTemp <- current.next // save next before overwriting7 current.next <- prev // flip the pointer8 prev <- current // advance prev9 current <- nextTemp // advance current1011 head <- prev // prev is now the new head
Array vs Linked List
| Operation | Array | Linked List |
|---|---|---|
| Access by index | O(1) ✓ | O(n) ✗ |
| Insert at head | O(n) ✗ | O(1) ✓ |
| Insert at tail | O(1) amortized ✓ | O(n) without tail ptr |
| Delete at head | O(n) ✗ | O(1) ✓ |
| Memory layout | Contiguous | Scattered |
| Cache friendliness | Excellent ✓ | Poor ✗ |
| Dynamic resize | Copies entire array | No resize needed ✓ |
Time complexity
Constant time
Constant time
Linear time
Linear time
Linear time
Linear time
🎯 Quick Check
You have a singly linked list: A -> B -> C -> D. You want to delete node C efficiently. What do you need?
Singly Linked Lists: summary
Key points
- Keep a
tailreference to make appending O(1) too. - There's no index access:
list[5]doesn't exist; you walk five steps. - Draw boxes and arrows when writing pointer code; most bugs are lost
nextreferences.
Frequently asked questions
›Linked list vs array: which is better?
Arrays are faster for access by index and iteration (cache-friendly). Linked lists are better when you frequently insert or remove at the front or at a known node.
›How do you reverse a linked list?
Walk the list once, pointing each node's next back to the previous node. It's O(n) time and O(1) space.