Linked List
In one line
A linked list is a linear data structure where each element (node) points to the next, instead of using contiguous memory.
In simple words
A linked list stores data in nodes, where each node contains a value and a reference (pointer) to the next node. Unlike an array, the elements are not stored in contiguous memory — they are chained together by pointers. You walk the list by starting at the head and following next pointers until you reach the end (null).
Linked lists excel at insertions and deletions at the beginning (O(1)), because you just adjust pointers — no elements need to shift. However, random access by index is O(n) because you must traverse from the head. Arrays are the opposite: O(1) access but O(n) insertion at the front.
Variants include doubly linked lists (each node also points to the previous) and circular linked lists (the last node points back to the head). Linked lists are the foundation of stacks, queues, and many higher-level structures.
Example
class Node {
constructor(value) { this.value = value; this.next = null; }
}
class LinkedList {
constructor() { this.head = null; }
prepend(value) {
const node = new Node(value);
node.next = this.head;
this.head = node;
}
toArray() {
const arr = []; let cur = this.head;
while (cur) { arr.push(cur.value); cur = cur.next; }
return arr;
}
}
const list = new LinkedList();
list.prepend("c"); list.prepend("b"); list.prepend("a");
console.log(list.toArray()); // ["a", "b", "c"]Each Node holds a value and a next pointer. prepend() inserts at the head in O(1). toArray() walks the chain.
Related terms
Related Resources
Related Lessons
- Data Structures
Learn linked lists and other structures.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.