ConceptDifficulty: Intermediate1 min

ArrayList vs LinkedList: How Each Stores Its Elements

The same methods, and swapping one for the other can turn a one-second report into a one-minute one.

ArrayList and LinkedList both implement List, with the same methods, arguments and results. They differ in how they store elements: an ArrayList keeps them side by side in one array, and a LinkedList keeps each in a separate node that points to the next. That storage decides what each one is fast and slow at.

Swapping one for the other in a report changes no line of its logic, and can turn a report that runs in a second into one that runs in a minute.

How each list stores its elements
Compared onArrayListLinkedList
Stores elements inOne array, side by sideSeparate nodes, each pointing at the next
To find element 500Arithmetic on the addressWalk 500 links
To insert at the frontShift every element up oneChange two pointers
Memory per elementOne slotThe element plus two pointers
An array list is one contiguous block; a linked list is separate nodes joined by pointers

Neither is faster overall. Each is fast at what the other is slow at: an array can jump straight to any position, and a chain of nodes can gain a new link anywhere without moving the others.

So this is the clearest case in the library for choosing on cost rather than on API. The methods tell you nothing, because they are identical by design; which operations your code does most tells you everything.