๐ What Is a LinkedList?
A LinkedList in Java is a linear data structure where elements (called nodes) are connected using references (links).
Each node stores:
The data itself, and
A reference (or pointer) to the next (and previous) node in the sequence.
In Java, LinkedList is a doubly-linked list โ meaning each node knows both its previous and next neighbor.
Youโll find it in the Java Collections Framework:
import java.util.LinkedList;
โ๏ธ Example
import java.util.LinkedList;
public class Example {
public static void main(String[] args) {
LinkedList<String> tasks = new LinkedList<>();
tasks.add("Write code");
tasks.add("Test code");
tasks.add("Deploy app");
System.out.println(tasks); // [Write code, Test code, Deploy app]
tasks.addFirst("Plan feature");
tasks.addLast("Refactor");
System.out.println(tasks); // [Plan feature, Write code, Test code, Deploy app, Refactor]
tasks.remove("Test code");
System.out.println(tasks); // [Plan feature, Write code, Deploy app, Refactor]
}
}
๐งฉ How It Works Internally
A LinkedList consists of nodes linked together.
Each node contains:
[ previous | data | next ]
When adding/removing elements:
ArrayList shifts elements โ O(n)
LinkedList just changes links โ O(1) (if position is known)
However, random access like get(index) is slower โ O(n).
โ๏ธ Common Methods
| Method | Description |
|---|---|
add(E e) |
Adds an element at the end |
addFirst(E e) / addLast(E e)
|
Adds at beginning or end |
remove() / removeFirst() / removeLast()
|
Removes elements |
get(int index) |
Retrieves element by index |
getFirst() / getLast()
|
Retrieves first or last element |
size() |
Returns list size |
clear() |
Removes all elements |
โ๏ธ LinkedList vs ArrayList
| Feature | ArrayList | LinkedList |
|---|---|---|
| Structure | Dynamic array | Doubly-linked list |
| Access time | O(1) | O(n) |
| Insert/remove (middle) | O(n) | O(1) if node known |
| Memory usage | Less | More (extra pointers) |
| Use case | Frequent access | Frequent insert/delete |
โ Rule of thumb:
Choose ArrayList โ when you read data often.
Choose LinkedList โ when you insert or remove elements often.
๐ Real-World Use Cases
Music players: Playlist queue (next/previous song navigation)
Undo/Redo systems: Navigating previous and next actions
Web browsers: Forward and backward page navigation
Task scheduling: Maintaining dynamic to-do or job queues
Memory management systems: Storing free/used memory blocks dynamically
Example:
LinkedList<String> browserHistory = new LinkedList<>();
browserHistory.add("Google");
browserHistory.add("StackOverflow");
browserHistory.add("GitHub");
โก Pro Tips
Use LinkedList for queues and stacks โ it implements both Deque and List interfaces.
Deque<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
System.out.println(queue.remove()); // 1
- Avoid random access like
list.get(1000)โ itโs slow. - Can contain null elements.
- Not thread-safe, so use
Collections.synchronizedList()if needed.
๐งฎ Performance Summary
| Operation | Time Complexity |
|---|---|
| Add/Remove First | O(1) |
| Add/Remove Last | O(1) |
| Add/Remove Middle | O(n) |
| Get by Index | O(n) |
| Iteration | O(n) |
๐ก Final Thought
Think of LinkedList as a flexible train โ you can easily attach or detach wagons (nodes) without rearranging the entire train.
It might not be as fast to jump between wagons, but itโs perfect when the structure itself keeps changing.
Top comments (0)