Difference Between ArrayList and LinkedList

Naveen AnamaneniUpdated · 5 min read

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

An array jumps straight to a slot; a linked list walks from node to node
  • ArrayList: elements sit side by side in one array. The position of element i is calculated directly, so get(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

ArrayListLinkedList
Internal structureResizable arrayDoubly linked list of nodes
get(i) / set(i, e)O(1), fastO(n), walks the nodes
add(e) at the endO(1) on averageO(1)
Add or remove at the beginningO(n), shifts every elementO(1)
Add or remove in the middleO(n), shiftingO(n) to find the position, then O(1)
Memory per elementLow: one array slotHigh: a node with two extra links
ImplementsList, RandomAccessList, Deque
Use as queue or stackNoYes
Best forReading by index, iterating, appendingFrequent 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.

ListTiming.java▶ Try it
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");
    }
}
Output
Add 50000 at the front:
  ArrayList  62 ms
  LinkedList 1 ms
Get 50000 by index:
  ArrayList  0 ms
  LinkedList 903 ms

Explanation: 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

  1. Easy: Change the timing program to remove 50,000 elements from the front of each list. Which is faster, and why?
  2. Harder: Time iterating over both lists with a for-each loop. Explain why both are fast, unlike get(i) on a LinkedList.

Quick check: ArrayList vs LinkedList

4 questions
1Which list gives O(1) get(i)?
2Which is faster at adding at the beginning?
3Which uses more memory per element?
4Which also implements Deque?

Key Takeaways

Was this tutorial helpful?

Lesson 263 of 482