Arrays vs linked lists
A playlist app with a million songs. Adding to the top, jumping to song 500,000, pressing back: each one is slow with the wrong structure. Pick the fix and watch it play out.
3 steps · about 5 minutes · free, no sign-up
Step 1: Add to the top
Your music app keeps a 1,000,000-song playlist in an array.
A new song goes to the top, so every song shifts one place to make room.
Every new song goes to the top of the playlist, and with an array that moves all 1,000,000 songs each time. What do you do?
- Use a bigger array (Not this one): Size isn’t the problem. Even with room to spare, inserting at the front still shifts every song after it.
- Link each song to the next one (a linked list) (Right fix): Each song just remembers which song comes next. A new top song points at the old first song, and the head moves to it: one step, however long the playlist. Linked list: A chain of items where each points to the next. Adding or removing at the front only changes a pointer, so it is one step (O(1)), with no shifting.
- Leave empty space at the front of the array (Helps for a while): Spare slots at the front make adding instant for a while. When they run out, you have to shift everything again, so it only delays the problem.
Step 2: Jump to song 500,000
Shuffle picked song 500,000. A list can only walk from the head, one link at a time…
…500,000 links later, the song starts.
Shuffle picks song 500,000, and the linked list has to follow 500,000 links from the head to reach it. What do you do?
- Follow the links faster (Not this one): Each step is already quick. The problem is the number of steps: 500,000 of them, every time.
- Walk from whichever end is closer (Helps for a while): With links in both directions you can start from the end, which halves the worst case. But the middle of the list is still 500,000 steps away.
- Use an array, which jumps straight to any position (Right fix): An array works out where song 500,000 lives (start + 500,000 × size) and jumps there in one step. Shuffle wants jumping, so it wants an array, even though adding at the top is slower. Random access: Arrays store items side by side, so the address of any position is simple arithmetic: O(1) access. Linked lists must walk: O(n). Pick the structure for the operation you do most.
Step 3: The back button
The up-next queue is a linked list. You press ◀ previous on "Fly"…
…but links only point forward, so it walks from the head to find the song before.
In the linked-list queue of up-next songs, “previous song” has to walk from the head to find the song before this one. What do you do?
- Remember the last 10 songs played (Helps for a while): Pressing back 10 times is instant now. The 11th press is slow again, and you keep a second structure in sync.
- Give each song a link back to the previous one (Right fix): With a pointer in each direction, “previous” is one step, just like “next”. Removing the current song is one step too, because it knows both neighbours. Doubly linked list: Each item points to the next and the previous one. It costs one extra pointer per item, and makes going back, and removing an item you are holding, one step.
- Start from the top and walk again (Not this one): That is exactly what it does now: hundreds of thousands of steps for one press.
What you learned
- Array: Items side by side: jump to any position in one step, but inserting in the middle or front shifts everything after it.
- Linked list: Items point to the next: add or remove at the front in one step, but reaching position n means walking n links.
- Doubly linked list: Links both ways: going back and removing an item you hold are one step.
- The trade-off: Choose by the operation you do most: jumping by position (array) or changing the ends (linked list).
| Operation | Array | Linked list |
|---|---|---|
| Get item by position | O(1) | O(n) |
| Add or remove at the front | O(n) | O(1) |
| Add at the end | O(1) amortised | O(1) with a tail pointer |
| Search by value | O(n) | O(n) |
| Memory | Compact, cache-friendly | One or two extra pointers per item |
In practice arrays (Python list, Java ArrayList) win most of the time because memory side by side is very fast to read. Use linked structures when you add and remove at the ends a lot, for example a queue (Python deque).
In code
# Array: Python's list
songs = ["Hey", "Yes", "Run"]
songs.insert(0, "New") # O(n): shifts everything
print(songs[2]) # O(1): jump to a position
# Linked list
class Node:
def __init__(self, value, next=None):
self.value, self.next = value, next
head = Node("Hey", Node("Yes", Node("Run")))
head = Node("New", head) # O(1): add at the front
# Fast at both ends: collections.deque
from collections import deque
q = deque(["Hey", "Yes"]); q.appendleft("New"); q.pop()- Lesson: why hash tables exist
- Lesson: binary search trees
- Read: arrays vs linked lists, when to use each
Frequently asked questions
When should I use a linked list instead of an array?
When you mostly add and remove items at the ends (or next to an item you already hold) and rarely jump to a position. Queues and undo histories are classic cases.
Why is array access O(1)?
Items sit side by side in memory, so the address of position i is start + i × item size: one calculation, no matter how long the array is.
Why is inserting at the front of an array O(n)?
Every existing item has to move one place to make room, so the work grows with the number of items.
Is a Python list an array or a linked list?
A dynamic array. For fast adds and removes at both ends, Python has collections.deque.