Quick revision
The short answer
An array keeps its elements side by side in one block of memory, so reading any index is a single address calculation, O(1), but inserting or removing in the middle shifts elements, O(n), and its size is fixed. A linked list keeps each element in its own node with a reference to the next, so inserting or removing at a node we already hold is O(1), but reaching index i means walking i links, O(n), and every node costs extra memory.
The solution
What it costs
| Operation or property | Array | Linked list |
|---|---|---|
| Read element i | O(1): address = start + i * size | O(i): follow i references |
| Insert or remove at the front | O(n): shift every element | O(1): change the head |
| Insert or remove at a node we hold | O(n): shift the rest | O(1): relink the neighbours |
| Insert at index i | O(n): shift the rest | O(n): walk to i first |
| Append at the end | O(1) amortized in a dynamic array | O(1) if we keep a tail reference |
| Binary search when sorted | O(log n) | no fast version, no random access |
| Memory per element | just the value | value plus references and a header |
| Going through every element | fast, cache friendly | slower, each hop may miss the cache |
| Size | fixed, or grows by copying | grows one node at a time |
What to remember
We compared arrays and linked lists by starting from how each sits in memory, like a block of seats in a cinema against friends scattered across the hall with notes. An array's single block gives O(1) access by index and fast, cache friendly loops, but inserting or removing shifts elements and growing means copying. A linked list's separate nodes give O(1) insertion and removal at a node we already hold, but reaching index i is a walk of i steps and every node costs extra memory. The clue to remember is the operation the problem repeats: jumping to positions points to an array, and changing the order at places we already hold points to a linked list.
1Two ways to keep things in order
Imagine taking a group of friends to the cinema. One way is to book a block of seats together in one row, seats 1 to 5. If someone asks where the fourth friend sits, you answer at once, seat 4, without looking at anyone else. Another way is to let everyone sit wherever there is a free seat, scattered over the hall, and give each friend a note saying where the next friend is sitting. Now to find the fourth friend you go to the first, read the note, go to the second, read the note, and so on.
The block of seats is an array, and the scattered friends with notes are a linked list. Keep this picture in mind, because every question about the two, how fast we find someone, how hard it is to let a new friend in, how much paper we need, has its answer in the cinema.
Let us give the two their proper names, one at a time. An array is a fixed number of slots of the same type, laid out one after another in a single block of memory. In Java, new int[3] reserves room for three ints in a row, and its length can never change afterwards. Each slot has a number called its index, starting from 0.
A linked list is a chain of separate objects called nodes. Each node holds one value and a reference called next, which is how Java names the node that comes after it, like the note in a friend's hand. The list itself remembers only the first node, called the head, and the last node's next is null, meaning there is nobody after it. In a doubly linked list, each node also remembers the node before it, through a reference usually called prev.
Your turn
Solve it before you read on. Start by writing one small example and the result you expect for it. If the question leaves something open, such as an empty input or repeated values, note what you would ask the interviewer. Say your approach aloud or type it below, and write the code in your own editor. Then take one hint at a time and compare.
2Looking inside an array
Because the slots of an array sit side by side and are all the same size, the machine can work out where any slot is with simple arithmetic. If the block starts at some address, and each int takes 4 bytes, then slot i starts at the start address plus 4 times i. The picture uses made-up addresses, a block starting at 1000, so the three slots start at 1000, 1004 and 1008.
The addresses are made up for the example, a block starting at 1000. Each int takes 4 bytes, so the slots start at 1000, 1004 and 1008.
- start
- 1000
- slot size
- 4 bytes
- length
- 3
1new int[] {20, 30, 40} reserves one block with three slots side by side. Slot 0 starts at address 1000, slot 1 at 1004, slot 2 at 1008.
- start
- 1000
- address
- 1000 + 2 * 4
- equals
- 1008
- arr[2]
- 40
2To read arr[2], the machine works out start + 2 * 4 = 1008 and reads that one place. It never looks at slots 0 or 1.
Notice what the second frame does not do. To read arr[2], the machine never visits slot 0 or slot 1; it multiplies, adds, and reads one place. That costs the same whether the index is 2 or 2 million, which is why reading an array element is O(1), meaning constant time. This ability to jump straight to any position is called random access, and it is the array's great strength.
It is also the reason for the array's great weakness. The formula only works if the slots really are side by side, with no gaps, in one block. So an array cannot simply grow, because the memory just after the block may already belong to something else, and it cannot open a gap in the middle without moving the elements that follow. Back in the cinema, the block of seats is easy to find but hard to change.
3Looking inside a linked list
A linked list makes the opposite choice. Each node is a separate object that Java places wherever there is room, so the nodes are scattered like the friends in the cinema. The picture shows the list 20 -> 30 -> 40 with made-up addresses beside it, to make the point that node 30 is not next to node 20 in memory at all. The arrows are the only thing that holds the list together.
Each box is a separate node object with its own place in memory. The arrows are the next references; the addresses on the right are made up for the example.
- node 20 at
- 5120
- node 30 at
- 1984
- node 40 at
- 7344
- 40.next
- null
1The list 20 -> 30 -> 40 is three node objects. The list itself only remembers the head; each node remembers only the node after it.
A beginner often asks at this point how the program knows the order, if the nodes are scattered. The answer is that the order lives entirely in the next references. Node 20 knows where node 30 is; node 30 knows where node 40 is; node 40 knows that nobody follows. Change an arrow and you change the order, without moving a single value. That is the linked list's great strength.
And there is the matching weakness. Since the nodes are not side by side, there is no formula for where node i is. The only way to reach it is to start at the head and follow the arrows, exactly as we had to visit friend after friend in the cinema.
4Finding the element at a position
Let us see the cost of that walk on a small list. The picture below shows get(head, 3) from the Java program at the end, reading index 3 of 10 -> 20 -> 30 -> 40. A pointer n, a variable that refers to one node, starts on the head, and each hop follows one next reference.
The pointer n starts on the head and follows next once per hop.
- k
- 0
- n.val
- 10
- hops
- 0
1
get(head, 3)starts with n on the head, node 10. A linked list has no address formula, so the only way to index 3 is to walk.- k
- 1
- n.val
- 20
- hops
- 1
2First hop: n = n.next moves n to node 20. We are at index 1 and still have two hops to go.
- k
- 2
- n.val
- 30
- hops
- 2
3Second hop: n moves to node 30, index 2. Each hop reads one node's next reference to find the following node.
- k
- 3
- n.val
- 40
- hops
- 3
4Third hop: n reaches node 40, index 3, and get returns 40. Reading index i took i hops, so the cost grows with i.
Reading index 3 took three hops, and reading index i takes i hops, so for a list of n nodes it is O(n) in the worst case, meaning the work grows in step with the length. Compare that with the array's single calculation. This is also why binary search, which keeps jumping to the middle of a sorted range, works beautifully on a sorted array in O(log n) but has no fast version on a linked list: every jump to the middle would be a walk.
There is one more effect that big O does not show, and interviewers like to hear it. A processor does not read memory one number at a time; it loads a small block, typically 64 bytes, into a fast memory called the cache. When we read one array element, its neighbours come along for free, so a loop over an array flies. Walking a linked list jumps from one scattered node to the next, and each hop can miss the cache and wait for slow main memory. So even though going through every element is O(n) for both, the array is often several times faster in practice.
5Inserting at the front of an array
Now let a new friend join at the front of the row. In the cinema everyone in the block must move one seat to the right to make room, and if the block is full, the whole group must find a bigger block and move there. An array is exactly the same. The picture follows insertFront from the Java program as it puts 10 in front of 20, 30, 40.
An array cannot grow and its slots cannot move, so insertFront builds a new, longer array and copies every element one slot to the right.
- length
- 3
1We start with the array 20, 30, 40. There is no free slot in front of 20, and the array's length is fixed at 3.
- length
- 4
- copied
- 0
2new int[4] reserves a new block one slot longer. Java fills a fresh int array with zeros.
- length
- 4
- copied
- 3
3
System.arraycopycopies 20, 30 and 40 into slots 1, 2 and 3. Every old element had to be copied once, which is the O(n) part.- length
- 4
- copied
- 3
4b[0] = 10 fills the free slot, and the new array 10, 20, 30, 40 is returned. Three moves for three elements; a million elements would mean a million moves.
Every existing element was copied once, so inserting at the front of an array of n elements is O(n). Inserting in the middle shifts everything after the position, and removing shifts everything left to close the gap, so both are O(n) too. Only working at the very end is cheap, as long as there is a spare slot.
Java's ArrayList is built on exactly this kind of array, with a little cleverness. It keeps some spare slots at the end, so adding at the end usually just fills one. When the spare slots run out, it makes a new array about one and a half times larger and copies everything across. Those copies are rare enough that, averaged over many additions, each one costs O(1); we call that amortized O(1). Adding at the front of an ArrayList still shifts everything, O(n).
6Inserting at the front of a linked list
For the scattered friends, a new friend can sit in any free seat. We give the newcomer a note pointing at the old first friend, and then tell everybody that the newcomer is now first. Nobody else moves. The picture shows the same two steps in the Java program, new Node(10, head) and then head = the new node.
No existing node moves. We make one new node and change one reference, however long the list is.
- head
- node 20
1The list is 20 -> 30 -> 40, and head refers to node 20.
- head
- node 20
- new node
- 10, next = node 20
2new
Node(10, head)makes one new node whose next refers to node 20. Nodes 20, 30 and 40 are not touched or copied.- head
- node 10
- moved
- 0 elements
3head = the new node moves one reference, and the list now reads 10 -> 20 -> 30 -> 40. That was two small steps, the same for any length.
That was one new node and one changed reference, whatever the length of the list, so inserting at the front of a linked list is O(1). Inserting after a node we already hold is the same: the new node's next takes the old node's next, and the old node's next takes the new node. Removing the node after one we hold is a single reference change. The list also grows one node at a time, so there is never a big copy.
Here comes the most common beginner mistake in this whole topic, so let us look at it slowly. People say that inserting into a linked list is O(1), and then use that to argue that a linked list is better for inserting at position i. But the O(1) only begins once we are standing at the right node. If all we know is the index i, we must first walk there, which is O(i). So inserting at an index is O(n) for both structures; the linked list only wins when the program already holds a reference to the node, as in the LRU cache, or when it works at the ends.
7What each one costs in memory, side by side
An int array uses 4 bytes per element plus a small header for the whole array, and nothing else. A linked list pays for every element separately. Each node is an object with its own header and one or two references besides the value. In Java's LinkedList each node holds a reference to the element, to the next node and to the previous one, roughly 24 bytes on a typical 64-bit JVM, and because a collection stores objects, each int is also boxed into an Integer of about 16 bytes. So a LinkedList of a million Integers uses around 40 MB, where an int[] of the same size uses about 4 MB. In the cinema, the block of seats needs no notes at all.
The table collects everything we have found, with the reason next to each cost, because the reason is what we should say aloud in an interview.
| Operation or property | Array | Linked list |
|---|---|---|
| Read element i | O(1): address = start + i * size | O(i): follow i references |
| Insert or remove at the front | O(n): shift every element | O(1): change the head |
| Insert or remove at a node we hold | O(n): shift the rest | O(1): relink the neighbours |
| Insert at index i | O(n): shift the rest | O(n): walk to i first |
| Append at the end | O(1) amortized in a dynamic array | O(1) if we keep a tail reference |
| Binary search when sorted | O(log n) | no fast version, no random access |
| Memory per element | just the value | value plus references and a header |
| Going through every element | fast, cache friendly | slower, each hop may miss the cache |
| Size | fixed, or grows by copying | grows one node at a time |
If you remember only two rows, remember the highlighted ones. The array wins at finding by position, and the linked list wins at changing the order at a place we already hold. Every other row follows from those two and from the cinema.
8Recognising which one a problem needs
In coding problems the choice follows from the operation the problem repeats most often. Words such as the element at index i, the middle, both ends moving inwards, a window sliding along, or a running total up to position i all lean on jumping to a position, so they point to an array. Binary search, two pointers from both ends, sliding windows, prefix sums and dynamic programming tables are all array techniques. On a linked list each index would become a walk from the head, and an O(n) algorithm could quietly become O(n squared).
Other words point to a linked list. A problem may hand us nodes rather than indexes, or ask us to insert or remove at a place we already hold, to splice two sequences together, or to move an element to the front in O(1). Linked list questions come with their own tools, such as rewiring with prev, curr and next to reverse a list, fast and slow pointers to find the middle or a loop, and a dummy node in front of the head to simplify merges. And when a problem wants both fast lookup and O(1) reordering, we combine the two ideas, as the LRU cache does: a hash map for lookup, whose values point into a doubly linked list that keeps the order.
9What this looks like in Java
The program below makes every picture in this article run. It reads arr[2] with one calculation, inserts 10 at the front of the array by copying, inserts 10 at the front of a hand-made list with one new node, and reads index 3 of the list with three hops. Then Java's ArrayList and LinkedList reach the same results through very different work. The comment on each line shows what it prints.
Let us read main in the order it runs. arr[2] prints 40 after one address calculation. insertFront makes a new array one slot longer, copies the three elements one place to the right with System.arraycopy, and puts 10 in slot 0, which is why it prints [10, 20, 30, 40]. Then we build the list 20 -> 30 -> 40, and head = new Node(10, head) adds 10 at the front with one new node and one reference. get(head, 3) walks three links and prints 40.
The last lines do the same front insertion with the library classes. arrayList.add(0, 10) shifts the three elements inside its array, while linkedList.add(0, 10) only relinks the head, yet both print the same list, and reading index 2 of either gives 30. Inside, though, the ArrayList answered get(2) with one calculation and the LinkedList with a walk. In everyday Java, ArrayList is the default list and ArrayDeque the default stack and queue; both are arrays inside. LinkedList is rarely the faster choice, because of its memory cost and cache misses, so we reach for a linked list when we hold node references and need O(1) removal or reordering.
10Follow-up questions
When would you choose a linked list over an array?
When the program holds references to nodes and needs to remove or move them in O(1). The LRU cache is the classic example: a hash map finds the node, and a doubly linked list lets us unlink it and put it at the front with a few reference changes. Linked lists also help when elements must never move in memory because other parts of the program point at them, or when whole chains must be spliced together in O(1). For plain storage and going through elements in order, an array based structure is almost always the better choice.
Why is appending to an ArrayList amortized O(1) and not simply O(1)?
Most appends write into a spare slot at the end, which is O(1). When the array is full, ArrayList makes a new one about 1.5 times larger and copies everything, which is O(n) for that one append. Because the capacity grows by a fixed factor each time, these copies become rarer as the list grows, and their total cost over n appends is O(n). Spread over n appends, that is O(1) each on average. A single append can still be slow, which is why we say amortized rather than plain O(1).
What does a doubly linked list add, and what does it cost?
Each node also keeps a prev reference to the node before it. That lets us walk backwards, and, more importantly, remove a node we hold in O(1) without searching for the node before it, because the node already knows both neighbours. The price is one more reference per node and two more reference changes on every insert and remove. Java's LinkedList is doubly linked, which is why it can add and remove at both ends in O(1).
11Conclusion
We compared arrays and linked lists by starting from how each sits in memory, like a block of seats in a cinema against friends scattered across the hall with notes. An array's single block gives O(1) access by index and fast, cache friendly loops, but inserting or removing shifts elements and growing means copying. A linked list's separate nodes give O(1) insertion and removal at a node we already hold, but reaching index i is a walk of i steps and every node costs extra memory. The clue to remember is the operation the problem repeats: jumping to positions points to an array, and changing the order at places we already hold points to a linked list.
Open with
The core difference is how they sit in memory. An array is one contiguous block of equally sized slots, so the address of element i is the start plus i times the element size, and reading any index is O(1).
Then, if they want more
That also makes it cache friendly, because neighbouring elements are loaded together, so looping over an array is very fast in practice.
The downside is that inserting or deleting in the middle shifts every later element, which is O(n), and the size is fixed, so growing means allocating a bigger array and copying; ArrayList hides that with geometric growth, which makes appending amortized O(1).
A linked list stores each element in its own node with a reference to the next, and the nodes can be anywhere in memory. So reaching index i means following i references, O(n), and binary search has no fast version.
But if I already hold a reference to a node, inserting or removing there is just relinking, which is O(1), and the list grows one node at a time. Inserting at an index is still O(n), because I have to walk there first.
The price is memory, since each node has a header and references on top of the value, and in Java the values are boxed. In practice I default to ArrayList or ArrayDeque, and I use a linked list when I keep references to nodes and need O(1) removal or reordering, as in the doubly linked list inside an LRU cache.
About this answer
- Last updated
- 15 November 2026
- Written for
- Java 21, using only java.util
- Checked
- The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
- Where the question comes from
- Public interview write-ups, reworded by Scrivio; counted 70 times in that collection. How it is counted
- Official documentation
- LinkedList (Java 21), Arrays (Java 21), ArrayList (Java 21), java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does an array differ from a linked list?": https://scrivio-interview-library.netlify.app/data-structures/solutions/how-does-an-array-differ-from-a-linked-list-8d02f1/
Coding patterns, question 1 of 15
More data structure basics questions