1. What is a linked list?
A linked list is a chain of nodes, where each node holds a value and the address of the next node.
think of a linked list like a train 🚂
Each train compartment is connected to the next compartment.
Every compartment (node) contains:
Passengers or goods → the data
A connector to the next compartment → the next pointer
Example:
The last compartment is not connected to anything, so it points to null.
Just like you move through train compartments one by one, a linked list is also traversed node by node from the beginning to the end.
Array vs linked list — what's the difference?
Array– each item occupies a particular position and can be directly accessed using an index number.
Linked list – need to follow along the chain of element to find a particular element. A data item cannot be accessed directly.
Why do we even need linked lists?
- You already know arrays. So why invent something new?
- The problem with arrays is that their size is fixed. When you create an array, you must say upfront how big it is. And inserting or deleting items in the middle means shifting everything else — which is slow.
- A linked list solves this. It grows and shrinks freely, and inserting or deleting is fast because you just change a pointer, not shift everything .
Real world examples you already use:
🌐 Browser back/forward — each page you visit is a node. Clicking back just follows the previous pointer
🎵 Music player — each song links to the next and previous song
🖼️ Image viewer — previous and next buttons work because images are linked in a chain
Linked lists are probably the second most commonly used general purpose storage structures after arrays
Operations
- Find - Find a link with a specified key value.
- Insert - Insert links anywhere in the list.
- Delete - Delete a link with the specified value.
Full Java implementation
class Link {
public int iData;
public Link next;
public Link(int id) {
iData = id;
next = null;
}
public void displayLink() {
System.out.println(iData);
}
}
class LinkList {
private Link first; // reference to the first node
public LinkList() {
first = null; // empty list at the start
}
// Returns true if the list has no nodes
public boolean isEmpty() {
return (first == null);
}
// Print every node in the list
public void displayList() {
Link current = first;
while (current != null) {
current.displayLink();
current = current.next; // move to the next node
}
}
// Add a new node at the very beginning
public void insertFirst(int id) {
Link newLink = new Link(id);
newLink.next = first; // new node points to old first
first = newLink; // new node becomes the first
}
// Remove and return the first node
public Link deleteFirst() {
Link temp = first; // save reference to return
first = first.next; // second node becomes first
return temp;
}
}
create a list, display it, then delete everything:
class myList {
public static void main(String[] args) {
LinkList theList = new LinkList(); // create empty list
theList.insertFirst(23); // insert four items
theList.insertFirst(89); // note: each goes to the FRONT
theList.insertFirst(12); // so the order will be reversed
theList.insertFirst(55);
theList.displayList(); // prints: 55, 12, 89, 23
while (!theList.isEmpty()) {
Link aLink = theList.deleteFirst();
System.out.print("Deleted: ");
aLink.displayLink();
}
}
}
Here is Bonus tip
Two advanced types: double-ended and doubly linked
Quick summary — what to remember
- A linked list is a chain of nodes. Each node holds data and a reference to the next node.
- first is the head. The last node's next is null.
- You cannot jump to index 5 directly — you must walk from first.
- Inserting at the front (insertFirst) is very fast — O(1).
- Searching takes O(n) — you check each node one by one.
- A doubly linked list lets you go both forward and backward.
- Linked lists are the foundation for stacks, queues, and graphs.





Top comments (0)