Linked Structures
Build a Singly Linked List
Construct a singly linked list by allocating one node per value and
chaining next references. Establishes the node + head + tail model used
by every later linked-list lesson.
Algorithm
Canonical input [10, 20, 30, 40] builds the chain
head -> 10 -> 20 -> 30 -> 40 -> null with one node appended per step.
Basic Implementation
basic.js
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
const values = [10, 20, 30, 40];
let head = null;
let tail = null;
for (const v of values) {
const node = new Node(v);
if (head === null) {
head = node;
} else {
tail.next = node;
}
tail = node;
}
Complexity
- Time: O(n) with a tail pointer
- Space: O(n) for the chain
Implementation notes
- JavaScript: a small
class Nodeis the idiomatic Node. - The replay never shows runtime references; nodes are labelled
node(<value>)and the chain view is rendered as10 -> 20 -> ... -> null.
node chain
Each `Node` carries a `value` and a `next` reference (or `null`).