If you are learning Java or preparing for coding interviews, understanding linked lists in Java is essential.
A linked list is one of the most fundamental data structures in programming and is widely used in software development, competitive coding, and data structure interviews. Unlike arrays, a linked list does not store elements in continuous memory locations. Instead, it stores data in connected nodes, with each node pointing to the next.
This makes insertion and deletion operations much more efficient in many scenarios.
In this guide, we will explain how linked lists work in Java, understand their internal structure, examine code examples, and explore their use in real-world applications.
What Is a Linked List in Java?
A linked list in Java is a linear data structure made up of nodes. Each node contains:
- data → the stored value
- next reference → points to the next node
Example:
Unlike arrays, linked lists are dynamic and grow as needed. This makes them ideal for applications that require frequent insertions and deletions.
Linked List in Data Structure in Java
In data structure terms, a linked list is a collection of connected nodes. Each node is an object that stores a value and a reference to the next node.
Example:
This structure enables dynamic memory allocation. Unlike arrays, elements are not stored side by side in memory.
Types of Linked Lists in Java
1) Singly Linked List
Each node points only to the next node.
This is the most commonly taught structure in DSA.
2) Doubly Linked List
Each node stores references to both previous and next nodes. This supports traversal in both directions. Java's built-in LinkedList uses this structure internally.
3) Circular Linked List
The last node points back to the first node.
Common use cases:
- Music playlists
- CPU scheduling
- Round-robin algorithms
How LinkedList Works in Java
Java's built-in LinkedList class is implemented as a doubly linked list. Each node stores the current value, a reference to the previous node, and a reference to the next node.
This enables:
- Insertion at both ends
- Deletion from both ends
- Bidirectional traversal
Representation:
This is why operations such as addFirst() and removeLast() are efficient.
Time Complexity of Linked List Operations in Java
This section is critical for technical and interview preparation.
| Operation | Time Complexity | Notes |
|---|---|---|
| Access by index | O(n) | Requires traversal from the head |
| Search | O(n) | Requires full traversal in the worst case |
| Insert at the beginning | O(1) | Only head pointer is updated |
| Insert at end | O(1) | Java's LinkedList maintains a tail reference |
| Delete at beginning | O(1) | Only head pointer is updated |
| Delete at end | O(1) | Java's LinkedList maintains a tail reference |
Note : Java's built-in LinkedList is a doubly linked list with a maintained tail reference, so both insert and delete at the end are O(1). Custom singly linked list implementations without a tail pointer would have O(n) delete at the end.
Linked List Node Class Java Example
Below is a linked list node class Java example:
class ListNode {
int value;
ListNode next;
ListNode(int value) {
this.value = value;
this.next = null;
}
}
This forms the foundation of a singly linked list.
Linked List Example in Java
Let us build a simple custom linked list:
public class LinkedListDemo {
public static void main(String[] args) {
ListNode first = new ListNode(10);
ListNode second = new ListNode(20);
ListNode third = new ListNode(30);
first.next = second;
second.next = third;
ListNode current = first;
while (current != null) {
System.out.print(current.value + " ");
current = current.next;
}
}
}
Output:
10 20 30
This is a clean linked list Java code example.
Java LinkedList Example Using Built-In Class
Java provides a built-in LinkedList inside the Collections Framework:
import java.util.LinkedList;
public class BuiltInLinkedList {
public static void main(String[] args) {
LinkedList<String> cities = new LinkedList<>();
cities.add("Pune");
cities.add("Mumbai");
cities.add("Bhopal");
System.out.println(cities);
}
}
Common LinkedList Methods in Java
| Method | Purpose |
|---|---|
| add() | Insert element at end |
| addFirst() | Insert at beginning |
| addLast() | Insert at end |
| remove() | Remove element |
| get() | Retrieve by index |
| contains() | Search element |
| size() | Get size |
| clear() | Remove all elements |
Example:
LinkedList<Integer> nums = new LinkedList<>();
nums.addFirst(10);
nums.addLast(30);
nums.add(20);
System.out.println(nums.get(1));
LinkedList Java Program: Insert Operation
class CustomLinkedList {
ListNode head;
void insertAtStart(int value) {
ListNode newNode = new ListNode(value);
newNode.next = head;
head = newNode;
}
}
Time complexity: O(1)
Linked List Java Code Example: Delete Operation
void deleteFirst() {
if (head != null) {
head = head.next;
}
}
This also runs in O(1).
Reverse Linked List in Java (Interview Section)
This is one of the most searched and asked interview questions:
ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
return prev;
}
This tests pointer logic, in-place reversal, and traversal skills.
Common Linked List Interview Problems in Java
These problems frequently appear in coding interviews:
- Reverse a linked list
- Detect cycle
- Find middle node
- Merge two sorted lists
- Remove nth node from end
- Reverse in groups
Mastering these significantly improves DSA interview readiness.
LinkedList vs ArrayList in Java
| Factor | LinkedList | ArrayList |
|---|---|---|
| Access by index | O(n) | O(1) |
| Insert / Delete at start | O(1) | O(n) |
| Insert / Delete at end | O(1) | O(1) amortized |
| Insert / Delete in middle | O(1) after traversal | O(n) |
| Memory usage | Higher (stores node pointers) | Lower |
| Dynamic size | Yes | Yes |
Use LinkedList for frequent insertions and deletions at the beginning or middle. Use ArrayList for faster random index-based access.
Real-World Use Cases of Linked List in Java
Linked lists are widely used in practical software systems because they efficiently handle dynamic data.
Browser Back and Forward Navigation
Browsers use linked structures to store page history. Each page links to the previous and next pages, making back-and-forward navigation seamless.
Music Playlists
Streaming apps use linked lists to manage playlists. Each song links to the previous and next tracks for easy navigation.
Task Scheduling
Operating systems use linked lists to manage tasks and processes. This helps add, remove, or reorder tasks dynamically.
Queue Systems
Linked lists are commonly used to implement queues. They allow fast insertion at the end and removal from the front.
Undo and Redo Operations
Editors and design tools use linked lists to track actions. This makes it easy to implement undo and redo features.
Memory Management
Some systems use linked lists to manage memory blocks. They help track free and allocated memory dynamically.
Advantages of Linked List in Java
1) Dynamic Size
Unlike arrays, the size of a linked list is not fixed. Elements can be added or removed at runtime without needing to define the size in advance.
2) Fast Insertions and Deletions
One of the biggest advantages of a linked list in Java is efficient insertion and deletion, especially at the beginning, middle, or a known node position. Since linked lists use node references, elements can be inserted or removed by updating pointers rather than shifting all elements as in arrays.
3) Efficient Memory Usage
Memory is allocated only when needed. Unlike arrays, which may reserve unused capacity, linked lists allocate memory dynamically for each node.
4) Useful for Dynamic Applications
Linked lists are widely used in applications where data changes frequently, including queues, stacks, browser history, music playlists, undo/redo systems, and task schedulers.
Disadvantages of Linked List in Java
1) Slower Random Access
Accessing a specific element requires traversal from the head node. For example, to access the 10th element, Java must traverse through the previous 9 nodes. This results in a time complexity of O(n), significantly slower than ArrayList's O(1) index-based access.
2) Extra Memory per Node
Every node stores an additional reference pointer. Singly linked list nodes each store data and a next reference. Doubly linked list nodes additionally store a previous reference. This means linked lists consume more memory compared to arrays.
3) More Complex Implementation
Compared to arrays, linked lists require more complex implementation logic. Operations such as insertion, deletion, reversal, and traversal involve pointer manipulation, making debugging slightly more challenging for beginners.
Conclusion
The linked list is a fundamental data structure that every Java learner should understand. It offers:
- Dynamic memory allocation
- Fast insertion and deletion
- Strong interview relevance
- Real-world use cases
Mastering both Java's built-in LinkedList class and custom implementations strengthens foundational knowledge in data structures and problem-solving. For those preparing for technical interviews, proficiency with linked lists is essential.

























