🌇DSA
← Back
Data StructureBeginner
🔗

Linked List

Connected nodes forming a chain

⏱️ 4–6 hours🧩 10 LeetCode
🔒 Login to unlock solutions and track progress. Login

What is it?

A linked list is like a treasure hunt. Each clue (node) tells you the value and where the next clue is.

  • Each node stores a value and a pointer to the next node.
  • Unlike arrays, nodes live anywhere in memory — not side by side.
  • Inserting at the front is O(1), but finding item #5 takes O(n) steps.

Variants / Subtypes

  • Singly Linked List — each node points forward only
  • Doubly Linked List — each node points both forward and backward
  • Circular Linked List — last node points back to the head

Real-World Examples

Music playlist (next song)
Browser back/forward
Train compartments
Undo history in editors
OS free-memory list
Implementing stacks & queues

Code Example

What this code shows

Build three nodes holding 1, 2, 3 and chain them together (1 → 2 → 3). Then walk the chain from head to tail, printing each value along the way.

class Node {
constructor(val) {
  this.val  = val;
  this.next = null; // pointer to next node
}
}

// create nodes and link them: 1 → 2 → 3
const a = new Node(1);
const b = new Node(2);
const c = new Node(3);
a.next = b;
b.next = c;

// traverse from head until null
let cur = a;
while (cur !== null) {
console.log(cur.val); // prints 1, then 2, then 3
cur = cur.next;       // move to next node
}

Expected Output

1
2
3

LeetCode Practice