Singly Linked Lists
Lesson 9 of 20Follow head and curr along a node chain — insert at the front in two moves and watch a middle node get bypassed.
The big picture
A linked list trades instant indexing for painless growth: each node knows only the next node, and every operation is just pointer rewiring.
A chain of nodes
Each Node holds one value and one next reference. There is no underlying array — the list exists only through its links.
head is everything
The single named reference into the chain. Lose head and every node after it becomes unreachable — the garbage collector cannot save you.
null ends the walk
The last node’s next is null. Every traversal loops while curr != null, then hops onto null to stop.
Fast at the front
Insert at head is two pointer moves — O(1) whether the list holds 3 nodes or 3 million.
Slow in the middle
Reaching position i costs i hops — O(n). ArrayList.get(i) is instant; LinkedList.get(i) walks the chain, hop by hop.
No shifting, ever
Arrays shove elements aside to insert; a list rewires two links instead. That is the trade for giving up random access.
Traverse & search
Six cached pages in a chain — finding 41 means walking from head, there are no shortcuts.
A 6-node chain — we are looking for 41. No indexes exist, so curr borrows head’s reference and starts walking.
The traversal flow
The while (curr != null) loop — the only way to move through a singly linked list.
Walkthrough
Building a chain by head insertion, then counting it — every move is a pointer assignment.
1public class BuildList {2static class Node {3int value;4Node next;5Node(int v) { value = v; }6}7static Node head;89static void insertAtHead(int v) {10Node node = new Node(v);11node.next = head;12head = node;13}1415public static void main(String[] args) {16insertAtHead(30);17insertAtHead(45);18insertAtHead(12);19System.out.println(head.value);20System.out.println(head.next.value);21}22}
head = null — an empty list. This one reference is the only way into the chain.
Insert & delete live
Head insertion in two moves, head deletion in one — then watch why tail insertion is the expensive direction.
Start small: one node holding 50. head is the only named reference into the chain.
Predict the output
Chained next expressions build the list — then the counter walks it.
1public class Predict {2static class Node {3int value;4Node next;5Node(int v) { value = v; }6}78public static void main(String[] args) {9Node head = new Node(3);10head.next = new Node(8);11head.next.next = new Node(15);12Node curr = head;13int count = 0;14while (curr != null) {15count++;16curr = curr.next;17}18System.out.println(count);19System.out.println(head.next.next.value);20}21}
Hint: head.next is the second node, head.next.next the third. The loop counts every node until curr lands on null.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
A singly linked list is nodes + one head reference — there is no underlying array.
Head insert and head delete are O(1): two pointer moves, nothing shifts.
Reaching position i costs i hops — LinkedList.get(i) is O(n), unlike array indexing.
Every traversal is while (curr != null), hopping with curr = curr.next.
Save curr.next before rewiring a node — one lost link and the rest of the chain is unreachable.
Losing head loses the list; an accidental cycle makes traversal run forever.