Java LinkedList
Explore a list that also works as a deque.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind LinkedList.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
LinkedList implements both List and Deque. End operations such as addFirst and removeFirst make its behavior easy to explore. It is not automatically faster than ArrayList for every insert or delete; traversal, memory use, and locality also matter.
Explore the details
Java LinkedList
In the previous chapter, you learned about the ArrayList class. The LinkedList class is
almost identical to the
ArrayList:
ArrayList vs. LinkedList
The LinkedList class is a collection which can contain many objects of the same type,
just like the ArrayList.
The LinkedList class has the same methods as ArrayList because both follow the List interface.
This means you can add, change, remove, or clear elements in a LinkedList just like you would with an ArrayList.
However, while the ArrayList class and the LinkedList class can be used in the same way,
they are built very differently.
How the ArrayList works
The ArrayList class has a regular array inside it. When an element is added, it is placed
into the array. If the array is not big enough, a new, larger array is created to replace the
old one and the old one is removed.
How the LinkedList works
The LinkedList stores its elements in "containers." The list has a link to the first container
and each container has a link to the next container in the list. To add an element to the list,
the element is placed into a new container and that container is linked to one of the other
containers in the list.
When To Use
Use an ArrayList for storing and accessing data, and LinkedList to
manipulate data.
LinkedList Methods
For many cases, the ArrayList is more efficient as it is common to need access to
random elements in the list, but the LinkedList provides several methods to do certain
operations more efficiently:
The var Keyword
From Java 10, you can use the var keyword to declare a LinkedList variable without writing the type twice.
The compiler figures out the type from the value you assign.
This makes code shorter, but many developers still use the full type for clarity.
Since var is valid Java, you may see it in other code, so it's good to know that it exists:
The List Interface
Note: Sometimes you will see both List and LinkedList in Java code, like this:
This means the variable (cars) is declared as a List (the interface), but it stores a LinkedList object (the actual list). Since LinkedList implements the List interface, this is possible.
LinkedList<String> cars = new LinkedList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
// LinkedList-specific methods
cars.addFirst("Mazda"); // insert at start
cars.addLast("Toyota"); // insert at end
cars.removeFirst(); // remove first element
cars.removeLast(); // remove last element
System.out.println(cars.getFirst()); // Volvo
System.out.println(cars.getLast()); // Ford
System.out.println(cars); // [Volvo, BMW, Ford]It works the same way, but some developers prefer this style because it gives them more flexibility to change the type later.
A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.util.LinkedList;
public class Main {
public static void main(String[] args) throws Exception {
LinkedList<String> queue = new LinkedList<>();
queue.addLast("Kavi");
queue.addLast("Malar");
queue.addFirst("Arun");
System.out.println(queue.removeFirst());
System.out.println(queue);
}
}Predict first. Then reveal the expected output / வெளியீடு
Arun [Kavi, Malar]
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Use peekFirst before removeFirst and compare the list contents afterwards.
Reveal the answer and reasoning / விடை
peekFirst reads without removing; removeFirst reads and removes. For a general queue or stack, also consider ArrayDeque.
One mistake to avoid: get(index) on LinkedList requires traversal. Do not assume constant-time random access.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.