Difference Between ArrayList and LinkedList
ArrayList and LinkedList both implement the List interface, so they have the same methods and both keep elements in insertion order. The difference is how they store the elements. ArrayList uses a resizable array, while LinkedList uses doubly linked nodes. This one difference decides which operations are fast and which are slow. In this lesson, you will learn all the differences and how to choose between them.
Internal Structure
- ArrayList: elements sit side by side in one array. The position of element
iis calculated directly, soget(i)is instant. - LinkedList: each element lives in a separate node object. To reach element
i, the list walks from the nearer end.
Differences Between ArrayList and LinkedList
| ArrayList | LinkedList | |
|---|---|---|
| Internal structure | Resizable array | Doubly linked list of nodes |
get(i) / set(i, e) | O(1), fast | O(n), walks the nodes |
add(e) at the end | O(1) on average | O(1) |
| Add or remove at the beginning | O(n), shifts every element | O(1) |
| Add or remove in the middle | O(n), shifting | O(n) to find the position, then O(1) |
| Memory per element | Low: one array slot | High: a node with two extra links |
| Implements | List, RandomAccess | List, Deque |
| Use as queue or stack | No | Yes |
| Best for | Reading by index, iterating, appending | Frequent changes at the ends |
Example: Measuring the Difference
Let's see an example that times two operations on both lists. Times change from run to run, so the output is only a sample.
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ListTiming {
static long time(Runnable task) {
long start = System.nanoTime();
task.run();
return (System.nanoTime() - start) / 1_000_000;
}
public static void main(String[] args) {
int n = 50_000;
List<Integer> array = new ArrayList<>();
List<Integer> linked = new LinkedList<>();
System.out.println("Add " + n + " at the front:");
System.out.println(" ArrayList " + time(() -> {
for (int i = 0; i < n; i++) array.add(0, i);
}) + " ms");
System.out.println(" LinkedList " + time(() -> {
for (int i = 0; i < n; i++) linked.add(0, i);
}) + " ms");
System.out.println("Get " + n + " by index:");
System.out.println(" ArrayList " + time(() -> {
for (int i = 0; i < n; i++) array.get(i);
}) + " ms");
System.out.println(" LinkedList " + time(() -> {
for (int i = 0; i < n; i++) linked.get(i);
}) + " ms");
}
}Add 50000 at the front:
ArrayList 62 ms
LinkedList 1 ms
Get 50000 by index:
ArrayList 0 ms
LinkedList 903 msExplanation: Adding at the front forces ArrayList to shift every element each time, while LinkedList just links a new node. Getting by index is the opposite: ArrayList jumps straight to the slot, while LinkedList walks up to 25,000 nodes for each get.
Common Mistakes
Picking LinkedList for "fast inserts" and then inserting by index. Finding the index is the slow part.
Using an index loop over a LinkedList. Use a for-each loop or an iterator.
Ignoring memory. A LinkedList of a million elements creates a million node objects.
Interview Corner
What is the difference between ArrayList and LinkedList?
ArrayList is backed by a resizable array, so access by index is O(1), appending is amortized O(1), and inserting or removing at the front or middle is O(n) because elements shift. LinkedList is a doubly linked list, so adding or removing at either end is O(1) but access by index is O(n) because it must walk the nodes. LinkedList uses more memory per element and also implements Deque. ArrayList is the better default in most situations.
When would you use LinkedList over ArrayList?
When the program frequently adds or removes elements at the beginning or end, or removes elements through an iterator while walking the list, and rarely reads by index. Even then, ArrayDeque is usually faster for queue and stack use.
Practice
- Easy: Change the timing program to remove 50,000 elements from the front of each list. Which is faster, and why?
- Harder: Time iterating over both lists with a for-each loop. Explain why both are fast, unlike
get(i)on aLinkedList.
Key Takeaways
Was this tutorial helpful?