Skip to content

Data Structures

Linked List

Add to the front: O(1) · Find an item: O(n), because you must walk the whole chain.

The idea, in plain English

A linked list is like a treasure hunt. Each clue is called a node. Each node holds one value, plus a note pointing to the next clue. You can't jump straight to clue #5. You must follow the notes from the first clue, one at a time.

How it works

  1. 1Each node stores a value and a link to the next node.
  2. 2The list only remembers the first node. This is called the head.
  3. 3To reach an item, start at the head and follow the links one by one.

When you'd use it

Use a linked list when you add and remove items often, and don't need to jump straight to item #500. It grows without shuffling every other item, unlike an array.

Common beginner mistakes

  • Don't lose the head reference. If you do, you lose the whole list.
  • Remember to update the .next links when you insert or remove a node. If you don't, you break the chain.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
List: 10 -> 20 -> 30
Head: 10

See it in motion

Watch it linkSingly linked listAdd to the front: O(1) · Find an item: O(n), because you must walk the whole chain.
102030head

A linked list: each box holds a value and a .next pointer to the following node.

3
Length
ready
Operation
1/12
Step
0.0s
Time
Cursor herePointer rewiredInserted / doneIdle .next

Not sure this is the right topic? See the learning paths → or where this leads →