Skip to main content

Linked List Operations

Prepend, append, and remove nodes, watch the pointers rewire in O(1) once you're in position.

time O(1)–O(n) per opspace O(n)Read the lesson
BeginnerO(1)–O(n) per op

Prepend, append, and remove nodes, watch the pointers rewire in O(1) once you're in position.

size 3

op 1 / 1

HEAD
4node 0
8node 1
15node 2
→ null

What's happening

Each node holds a value and a pointer to the next. HEAD marks the entry; following arrows walks the list, no random access.

size: 3last op: init

Prepend is O(1); append and remove walk the list first (O(n)) unless you hold a reference to the position, that's the array trade-off in reverse. The Back/Forward buttons scrub your operation history.

Understand the algorithm

time O(1)–O(n) per opspace O(n)

What it is

A linked list stores each element in a node with a pointer to the next node. Inserting or removing never shifts memory, only pointers are rewired.

Why it matters

It is the counterweight to arrays: arrays buy O(1) indexing at the price of O(n) middle edits; lists buy O(1) pointer surgery at the price of O(n) traversal and no indexing. Seeing the rewiring step by step is what makes the trade-off real.

How it works

  1. 1Each node holds a value and a next pointer.
  2. 2Prepend: new node's next points at the old head; head moves, O(1).
  3. 3Append (without a tail pointer): walk to the last node, link, O(n).
  4. 4Remove: walk to the node BEFORE the target, rewire its next past the target, O(n) to find, O(1) to rewire.

Real-world uses

  • Building blocks for stacks, queues, and hash-table collision chains
  • LRU caches (doubly-linked list + hash map)
  • Music playlists and undo chains where order with cheap splicing matters
  • Memory allocators' free lists

Strengths

  • O(1) insertion/removal once positioned, no shifting, ever
  • Grows and shrinks with no reallocation or capacity waste
  • The foundation of more advanced pointer structures

Limitations

  • No O(1) indexing, reaching position i is an O(i) walk
  • Each node pays pointer memory overhead
  • Cache-unfriendly: nodes scatter across memory, so traversal is slower than an array scan