Linked List

A sequence of nodes where each node stores data and a pointer to the next node. O(1) head insertion, O(n) random access. No wasted memory.

Syntax

dsa
class Node { data; next; }
class LinkedList { head; }

Example

dsa
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
// 1 -> 2 -> 3 -> null
// Traversal: O(n), Insert at head: O(1)