Java LinkedList – Complete Guide with Syntax, Examples & Use Cases
Introduction – Why Java LinkedList Is Important
Imagine you’re managing a dynamic playlist or task queue where items are frequently added or removed — an ArrayList would slow down due to shifting. This is where Java LinkedList shines.
The LinkedList class is part of Java’s Collections Framework, offering a doubly-linked list structure for efficient insertion and deletion.
By the end of this article, you’ll learn:
How Java LinkedList works and when to use it
Syntax, core methods, and real-world examples
Differences between LinkedList and ArrayList
Best practices and performance insights
What Is LinkedList in Java?
LinkedListin Java is a doubly-linked list implementation of theList,Deque, andQueueinterfaces.
It allows:
- Dynamic resizing
- Fast insertion/deletion at both ends
- Support for null, duplicates, and sequential access
Basic Syntax – Creating a LinkedList
import java.util.LinkedList;
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> colors = new LinkedList<>();
colors.add("Red");
colors.add("Green");
colors.add("Blue");
System.out.println(colors); // Output: [Red, Green, Blue]
}
}
LinkedList<String> declares a list of Strings
add() appends elements to the end
Insertion order is preserved
LinkedList Key Methods
| Method | Description | Example |
|---|---|---|
add(E e) | Add element to end | list.add("Orange") |
addFirst(E e) | Insert at the beginning | list.addFirst("Mango") |
addLast(E e) | Insert at the end | list.addLast("Peach") |
getFirst() | Get first element | list.getFirst() |
getLast() | Get last element | list.getLast() |
removeFirst() | Remove from front | list.removeFirst() |
removeLast() | Remove from end | list.removeLast() |
peek() | Retrieve head without removing | list.peek() |
poll() | Retrieve and remove head | list.poll() |
Iterating Over a LinkedList
Using for-each loop
for (String color : colors) {
System.out.println(color);
}
Using forEach() (Java 8+)
colors.forEach(System.out::println);
LinkedList as Queue and Stack
Queue Behavior (FIFO)
LinkedList<String> queue = new LinkedList<>();
queue.add("Task 1");
queue.add("Task 2");
System.out.println(queue.poll()); // Task 1
Stack Behavior (LIFO)
LinkedList<Integer> stack = new LinkedList<>();
stack.push(10);
stack.push(20);
System.out.println(stack.pop()); // 20
push()/pop() make LinkedList behave like a stack
add()/poll() behave like a queue
LinkedList vs ArrayList
| Feature | LinkedList | ArrayList |
|---|---|---|
| Underlying Structure | Doubly linked list | Dynamic array |
| Access time | Slower (O(n)) | Fast (O(1) for index) |
| Insert/Delete | Fast at start/mid (O(1)/O(n)) | Slower (due to shifting) |
| Memory usage | More (extra node pointers) | Less |
| Use case | Frequent insert/delete | Frequent read/access |
Real-World Example – Task Scheduler
LinkedList<String> tasks = new LinkedList<>();
tasks.add("Design UI");
tasks.add("Write API");
tasks.addFirst("Login");
System.out.println("First task: " + tasks.getFirst());
Add high-priority tasks at the front
Useful for job queues, undo stacks, playlists, etc.
Best Practices for Using LinkedList
Use when insertions/deletions are frequent
Avoid for random access-heavy operations
Use Deque interface methods (addFirst, removeLast) for stack/queue behavior
Avoid mixing ArrayList and LinkedList unless necessary
Summary
LinkedListis a versatile, doubly-linked structure in Java- Ideal for frequent insert/delete operations
- Can function as List, Queue, or Stack
- Offers built-in methods for both end-based and index-based manipulation
FAQs – Java LinkedList
Can LinkedList store null values?
Yes. It can store multiple null elements.
Is LinkedList synchronized?
No. Use Collections.synchronizedList() or ConcurrentLinkedDeque for thread safety.
How does LinkedList differ from ArrayList?
LinkedList offers faster insert/delete, but slower random access.
Can LinkedList be used as a Queue?
Yes. It implements the Queue and Deque interfaces.
What is the time complexity of adding/removing elements?
- Add/remove at ends: O(1)
- Add/remove in middle: O(n)
- Access by index: O(n)
Share Now :
