A linked list is a chain of items where each item knows where the next one is. Each item, called a node, stores a value and a link to the next node. You start at the first node and follow the links to reach the rest.
This page uses a train-coach analogy, a node diagram and a small Java program. If you have not met data structures before, start with what are data structures.
Linked list in simple words: the train analogy
Think of a train. It is made of coaches. Each coach is joined to the next coach by a coupling.
- Each coach carries passengers. That is the data.
- Each coupling joins a coach to the next one. That is the link.
- The engine is where the train starts. In a linked list, this is the head.
- The last coach has nothing behind it. In a linked list, the last node points to null.
To reach coach S5, you cannot jump there. You walk from the engine through S1, S2, S3 and S4. A linked list works the same way.
Adding a coach in the middle is easy. You uncouple two coaches, put the new one between them and couple it on both sides. No other coach moves. That is the big strength of a linked list.
The node diagram
A node has two parts:
+------+------+
| data | next |
+------+------+
Here is a list holding 10, 20 and 30:
head
|
v
+----+---+ +----+---+ +----+------+
| 10 | o-+---> | 20 | o-+---> | 30 | null |
+----+---+ +----+---+ +----+------+
headstores a reference to the first node.- Each
nextstores a reference to the following node. - The last node's
nextisnull, which marks the end.
The nodes can be anywhere in memory. They do not need to sit next to each other, because each node carries the address of the next one. In an array, values must sit side by side.
Linked list vs array basics
| Point | Array | Linked list |
|---|---|---|
| Memory layout | Values side by side | Nodes anywhere, joined by links |
| Size | Fixed in Java and C once created | Grows and shrinks one node at a time |
| Read the item at position i | O(1), jump straight to it | O(n), walk from the head |
| Add or remove at the front | O(n), other values must shift | O(1), change the head |
| Add or remove after a node you already have | O(n), values must shift | O(1), change one or two links |
| Extra memory | None per item | One reference per node (two in a doubly linked list) |
| Search for a value | O(n) if unsorted | O(n) |
Use an array (or ArrayList in Java) when you read by position often. Use a linked list when you add and remove at the ends or at known nodes often. For the full side-by-side view, read array vs linked list.
Interview note: "Insert in a linked list is O(1)" is only true when you already have the node to insert after, or when you insert at the head. Finding the right spot first can take O(n).
A small linked list in Java
This program builds a singly linked list by hand. It can add at the end, add at the front, delete a value and print the list.
public class LinkedListDemo {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static Node head;
static void addFirst(int value) {
Node node = new Node(value);
node.next = head;
head = node;
}
static void addLast(int value) {
Node node = new Node(value);
if (head == null) {
head = node;
return;
}
Node current = head;
while (current.next != null) {
current = current.next;
}
current.next = node;
}
static void delete(int value) {
if (head == null) return;
if (head.data == value) {
head = head.next;
return;
}
Node current = head;
while (current.next != null && current.next.data != value) {
current = current.next;
}
if (current.next != null) {
current.next = current.next.next;
}
}
static void print() {
Node current = head;
while (current != null) {
System.out.print(current.data + " -> ");
current = current.next;
}
System.out.println("null");
}
public static void main(String[] args) {
addLast(10);
addLast(20);
addLast(30);
print();
addFirst(5);
print();
delete(20);
print();
}
}
Output:
10 -> 20 -> 30 -> null
5 -> 10 -> 20 -> 30 -> null
5 -> 10 -> 30 -> null
Save it as LinkedListDemo.java and run java LinkedListDemo.java with Java 11 or later.
How each operation works
Add at the front
Before: head -> 10 -> 20 -> 30 -> null
Step 1: new node 5, set 5.next = head (10)
Step 2: head = 5
After: head -> 5 -> 10 -> 20 -> 30 -> null
Only two links change, no matter how long the list is. This is O(1).
Add at the end
The code walks from the head until it finds the node whose next is null, then links the new node there. Walking the list takes O(n). Many real implementations also keep a tail reference so that adding at the end becomes O(1).
Delete a value
To delete 20, the code stops at the node before it (10). Then it skips 20:
Before: 10 -> 20 -> 30
Change: 10.next = 20.next (which is 30)
After: 10 -> 30
Nobody points to 20 any more, so Java's garbage collector frees it. In C you must call free yourself.
Print the list
Printing starts at head and moves with current = current.next until current is null. This loop is the basic pattern for almost every linked list task.
Common mistake: Moving
headitself while printing, as inhead = head.next. After the loop, the list looks empty because you lost the start. Always use a separatecurrentvariable.
Types of linked lists
| Type | Each node has | Can move | Example use |
|---|---|---|---|
| Singly linked list | data, next | Forward only | Simple stacks and queues |
| Doubly linked list | data, prev, next | Forward and backward | Browser back and forward, Java's LinkedList |
| Circular linked list | data, next (last points to first) | Round and round | Taking turns in a loop, such as round-robin scheduling |
A doubly linked node looks like this:
+------+------+------+
| prev | data | next |
+------+------+------+
The extra prev link uses more memory but lets you delete a node when you only have that node, and walk backwards.
Java's built-in LinkedList
You rarely write your own linked list at work. Java gives you java.util.LinkedList, which is a doubly linked list.
import java.util.LinkedList;
public class BuiltIn {
public static void main(String[] args) {
LinkedList<String> coaches = new LinkedList<>();
coaches.add("S1");
coaches.add("S2");
coaches.addFirst("Engine");
coaches.addLast("Guard");
coaches.remove("S1");
System.out.println(coaches);
System.out.println(coaches.getFirst() + " " + coaches.size());
}
}
Output:
[Engine, S2, Guard]
Engine 3
In practice, ArrayList is the better default for most Java code because reading by index is fast. Our ArrayList vs LinkedList guide explains when each one fits.
Where linked lists are used
- Stacks and queues can be built on a linked list, adding and removing at the ends in O(1).
- Music playlists with next and previous song work like a doubly linked list.
- Undo history in some editors keeps steps as a chain.
- Hash tables in many languages keep items that land in the same bucket in a small linked list. Java's
HashMapdoes this (and can switch a very long bucket to a tree). - Memory managers often keep free blocks of memory in a linked list.
Linked lists in C
In C, the next link is a pointer:
struct Node {
int data;
struct Node *next;
};
You create nodes with malloc and release them with free. If pointers are new to you, read what is a pointer in C before writing a C linked list.
Practice problems for beginners
Try these in order. Draw the boxes and arrows on paper before you code each one.
- Count the nodes in a list.
- Find whether a value is present.
- Insert a value at a given position.
- Find the middle node.
- Reverse the list.
- Detect whether the list has a loop.
Reversing a list and finding the middle are among the most common interview questions on this topic. For the middle node, try two pointers: one moves one step, the other moves two steps.
Quick revision
- A linked list is a chain of nodes joined by links.
- Each node holds data and a reference to the next node.
- The head is the first node. The last node points to null.
- Reaching position i takes O(n). Adding at the head takes O(1).
- Java's
LinkedListis doubly linked.
When you can write addFirst, addLast, delete and print without looking, move on to stacks, queues and trees in our data structures and algorithms guide, then try the DSA interview questions.
Frequently Asked Questions
What is a linked list in simple words?
What is a node in a linked list?
What is the difference between an array and a linked list?
Does Java have a built-in linked list?
What are the types of linked lists?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Discover CodeBegun's Java Full Stack track

