Chapter 7 · Linked Structures

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

🔍Why nodes are scattered in memory
Arrays need a contiguous block big enough for all elements upfront. Linked list nodes are allocated individually — node 3 might be 10MB away from node 2. This is why you can 't jump to index 3 by arithmetic; you must follow the chain.

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: 4
head↓null10next*25next*7next*43next*
new / reversedtraversingfounddeleting

Core operations

singly-linked-list.pseudo
1Node: { value, next <- null }
2List: { head <- null, length <- 0 }
3 
4prepend(value): // O(1)
5 newNode <- Node(value)
6 newNode.next <- head
7 head <- newNode
8 length += 1
9 
10append(value): // O(n) — must reach tail
11 newNode <- Node(value)
12 if head = null: head <- newNode; return
13 current <- head
14 while current.next != null: current <- current.next
15 current.next <- newNode
16 
17deleteHead(): // O(1)
18 if head = null: error
19 head <- head.next
20 
21getAt(index): // O(n)
22 current <- head
23 for i from 0 to index-1: current <- current.next
24 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.

reverse.pseudo
1reverse(list):
2 prev <- null
3 current <- head
4 
5 while current != null:
6 nextTemp <- current.next // save next before overwriting
7 current.next <- prev // flip the pointer
8 prev <- current // advance prev
9 current <- nextTemp // advance current
10 
11 head <- prev // prev is now the new head
💡Three-pointer pattern
Always save current.next before overwriting it, or you lose the rest of the list. This three-pointer pattern (prev, current, nextTemp) solves reversal in O(n) time and O(1) space — no auxiliary array needed.

Array vs Linked List

OperationArrayLinked List
Access by indexO(1) ✓O(n) ✗
Insert at headO(n) ✗O(1) ✓
Insert at tailO(1) amortized ✓O(n) without tail ptr
Delete at headO(n) ✗O(1) ✓
Memory layoutContiguousScattered
Cache friendlinessExcellent ✓Poor ✗
Dynamic resizeCopies entire arrayNo resize needed ✓

Time complexity

Prepend (insert at head)O(1)

Constant time

Delete headO(1)

Constant time

Append (insert at tail — no tail pointer)O(n)

Linear time

Access by indexO(n)

Linear time

Search by valueO(n)

Linear time

ReverseO(n)

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?

🧠When linked lists genuinely win
Linked lists beat arrays when you need O(1) prepend or when elements are constantly inserted/deleted at the head. Implementing a stack or queue with a linked list (using only head operations) gives true O(1) for all core operations with no resize cost ever.

Singly Linked Lists: summary

Key points

  • Keep a tail reference 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 next references.

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.