Singly Linked List
What you'll build
A LinkedList class whose nodes hold a value and a next pointer, supporting append, prepend, delete, and toArray operations.
Goals
- Model a node as an object with value and next references
- Traverse a linked list by following next pointers
- Insert and remove nodes while keeping the chain intact
- Compare linked list trade-offs against arrays
Prerequisites
- Objects and property access
- Functions and return values
- while loops
Steps
- 1
Define the Node and List
A linked list is a chain of nodes. Each node stores a value and a reference to the next node. Create a Node helper and a LinkedList class with a head pointer that starts null.
Codeclass Node { constructor(value) { this.value = value; this.next = null; } } class LinkedList { constructor() { this.head = null; this.size = 0; } }Expected result
An empty LinkedList with head === null and size === 0.
- 2
Implement append
To append at the tail, walk from head to the last node (where next === null), then attach the new node. If the list is empty, the new node becomes the head.
Codeappend(value) { const node = new Node(value); if (!this.head) { this.head = node; } else { let current = this.head; while (current.next) current = current.next; current.next = node; } this.size++; return this; }Expected result
list.append(1).append(2).append(3) builds a chain 1 → 2 → 3.
- 3
Implement prepend
To prepend at the head, point the new node's next at the current head, then make the new node the head. This is O(1) — faster than append.
Codeprepend(value) { const node = new Node(value); node.next = this.head; this.head = node; this.size++; return this; }Expected result
list.prepend(0) makes 0 the new head, shifting the rest right.
- 4
Implement delete
To delete a value, walk the list keeping a reference to the previous node. When the current node matches, unlink it by pointing prev.next at current.next. Handle the head case separately.
Codedelete(value) { if (!this.head) return false; if (this.head.value === value) { this.head = this.head.next; this.size--; return true; } let prev = this.head; let current = this.head.next; while (current) { if (current.value === value) { prev.next = current.next; this.size--; return true; } prev = current; current = current.next; } return false; }Expected result
After delete(2), the chain skips the node whose value was 2.
- 5
Implement toArray and test
Add a toArray method that walks the list and collects values into an array, then run a full end-to-end test.
CodetoArray() { const result = []; let current = this.head; while (current) { result.push(current.value); current = current.next; } return result; } // Test const list = new LinkedList(); list.append(1).append(2).append(3); list.prepend(0); console.log(list.toArray()); // [0, 1, 2, 3] list.delete(2); console.log(list.toArray()); // [0, 1, 3] console.log('Size:', list.size); // 3Expected result
Output shows [0,1,2,3], then [0,1,3], then Size: 3.
Try it yourself
class Node { constructor(value) { this.value = value; this.next = null; } }
class LinkedList {
constructor() { this.head = null; this.size = 0; }
// TODO: append, prepend, delete, toArray
}class Node { constructor(v) { this.value = v; this.next = null; } }
class LinkedList {
constructor() { this.head = null; this.size = 0; }
append(v) {
const n = new Node(v);
if (!this.head) this.head = n;
else { let c = this.head; while (c.next) c = c.next; c.next = n; }
this.size++; return this;
}
prepend(v) {
const n = new Node(v); n.next = this.head; this.head = n; this.size++; return this;
}
delete(v) {
if (!this.head) return false;
if (this.head.value === v) { this.head = this.head.next; this.size--; return true; }
let p = this.head, c = this.head.next;
while (c) { if (c.value === v) { p.next = c.next; this.size--; return true; } p = c; c = c.next; }
return false;
}
toArray() { const r = []; let c = this.head; while (c) { r.push(c.value); c = c.next; } return r; }
}What you learned
You built a node-based data structure and practiced pointer manipulation — the foundation for trees, graphs, and other linked structures that arrays cannot easily represent.
Related Resources
Related Lessons
Related Glossary
- Linked List
A chain of nodes connected by references.
- Pointer
A reference to another node or object.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- Linked List
A chain of nodes connected by references.
- Pointer
A reference to another node or object.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.