DSA in Java: Linked Lists Explained (Beginner Guide)
Learn linked lists in Java from scratch: nodes and pointers, traversal, O(1) front insertion, arrays vs linked lists, and reversing a linked list.
Welcome to the "core data structures" section of the roadmap! We start with the linked list — the first structure that isn't a plain array, and a favourite in interviews. Linked lists teach you to think in terms of nodes and pointers, a mental model that unlocks trees, graphs, and much of what's ahead. Let's build one from scratch so it's never mysterious again.
The problem with arrays (and why linked lists exist)
Remember from the arrays lesson: arrays store items right next to each other in memory. That's great for fast index access, but it means inserting or deleting in the middle is slow (O(n)) — everything after has to shift. A linked list solves this differently: its items can live anywhere in memory, and each one simply points to the next.
Nodes: the building block
A linked list is a chain of nodes. Each node holds two things: a value, and a reference (pointer) to the next node. The last node points to null, marking the end.
In Java, a node is just a small class:
class Node { int value; Node next; // points to the next node (or null) Node(int value) { this.value = value; } }
That Node next field — a node referencing another node — is the whole idea. We keep a single reference called head pointing to the first node; from there we can reach every other node by following next.
Traversing the list
To visit every node, start at head and keep hopping along next until you hit null:
Node current = head; while (current != null) { System.out.println(current.value); current = current.next; // hop to the next node }
This "walk with a current pointer" loop is the backbone of almost every linked-list operation. Get comfortable with it.
Inserting at the front — the O(1) superpower
Adding a node at the start of a linked list is instant, no shifting required — just point the new node at the old head, then move head:
void addFirst(int value) { Node node = new Node(value); node.next = head; // new node points to old first node head = node; // head now points to new node — O(1)! }
This is exactly where linked lists beat arrays: front insertion is O(1), whereas inserting at the front of an array is O(n).
Arrays vs Linked Lists
| Operation | Array | Linked List |
|---|---|---|
| Access by index | O(1) ✓ |
O(n) — must walk |
| Insert/delete at front | O(n) |
O(1) ✓ |
| Memory | Compact | Extra pointer per node |
Neither is "better" — they trade off. Need fast random access? Array. Doing lots of front/middle insertions and deletions? Linked list. Choosing the right structure for the job is what DSA is really about.
The classic interview problem: reverse a linked list
Reversing a linked list appears in interviews constantly. The trick is to walk the list while flipping each node's next pointer backwards, using three pointers:
Node reverse(Node head) {
Node prev = null, current = head;
while (current != null) {
Node nextTemp = current.next; // remember what's ahead
current.next = prev; // flip the pointer backwards
prev = current; // move prev forward
current = nextTemp; // move current forward
}
return prev; // prev is the new head — O(n) time, O(1) space
}
LinkedList in the Collections framework, so you rarely hand-build one. But interviewers love raw linked-list problems precisely because they test whether you can manipulate pointers correctly — so learn the mechanics here.Linked lists were my personal turning point in DSA — the first time I had to truly picture pointers in my head rather than just index into an array. My advice: grab paper and draw the boxes and arrows as you trace reverse. Move prev and current along by hand, one node at a time. Pointer manipulation feels fiddly until you've drawn it a few times, and then it becomes second nature — and that same node-and-pointer thinking is exactly what makes trees and graphs (coming up) click.
Key takeaways
- A linked list is a chain of nodes; each holds a value and a
nextpointer, ending atnull. - Traverse with a
currentpointer that followsnextuntil null. - Front insert/delete is O(1) (vs O(n) for arrays), but index access is O(n) (vs O(1)).
- Reversing a linked list with three pointers (prev/current/next) is a must-know interview pattern.
← Previous: DSA Lesson 6 — Sorting Algorithms
Next: DSA Lesson 8 — Stacks & Queues →
↑ Back to the DSA roadmap