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.java
public class Basic {
static class Node {
int value;
Node next;
Node(int value) { this.value = value; }
}
public static void main(String[] args) {
int[] values = {10, 20, 30, 40};
Node head = null;
Node tail = null;
for (int v : values) {
Node 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
- Java: a small static inner
class Nodeis the idiomatic Node. - The replay never shows object identity; nodes are labelled
node(<value>)and the chain view is rendered as10 -> 20 -> ... -> null.
node chain
Each `Node` carries an `int value` and a `Node next` reference (or `null`).