Programming5 min read10 interview questionsNew · November
Linked lists vs arrays
Two ways to store a list of items, and why the difference matters for speed.
In one sentenceAn array keeps items side by side in memory so you can jump to any position, while a linked list chains items together by pointers.
Think of it like thisAn array is a row of numbered lockers. A linked list is a treasure hunt where each clue tells you where the next one is.
The big picture
Tap any step to see where to use it, how to use it and an example result.
Arrays
Because items are contiguous, reading by position is O(1) and scanning is fast because the processor loads neighbouring items together. Inserting or deleting in the middle means shifting everything after it, which is O(n). When a fixed array fills up, growing it needs a new, larger block.
Linked lists
Each node stores a value and a link to the next, and in a doubly linked list also to the previous. Adding or removing next to a node you already hold is O(1) because only links change. Finding the nth item, however, means walking from the start, which is O(n).
What Python does
A Python list is a dynamic array of references, not a linked list. Append is fast on average, and inserting at the front is slow. For fast adds and removals at both ends, use collections.deque, which is built from linked blocks.
Which to choose
In practice arrays win most of the time because of fast access and good use of the processor cache. Linked lists make sense when you often insert or remove at known positions and never need random access, or in building blocks such as queues and some memory managers.
Side by side
| Feature | Array | Linked list |
|---|---|---|
| Access by position | O(1) | O(n) |
| Insert in the middle | O(n) | O(1) at a known node |
| Memory | Compact | Extra pointer per item |
| Cache friendliness | Good | Poor |
| Typical use | Most lists and tables | Queues, frequent edits at known nodes |
Common mistakes
- Assuming a linked list is faster for insertion without counting the time to find the position.
- Forgetting the extra memory each link costs.
- Thinking a Python list is a linked list.
- Ignoring cache effects when comparing on paper.
Quick recap
- Arrays: fast access by position, slow insertion in the middle.
- Linked lists: fast insertion at a known node, slow access by position.
- Python lists are arrays; deque suits both ends.
- Arrays are usually the better default.
Try it yourself
Draw the list 10, 20, 30 as an array and as a linked list. Then draw what changes when you insert 15 after 10 in each.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is the difference between an array and a linked list?Basic
An array stores items in one block of memory and accesses them by index. A linked list stores nodes anywhere in memory, each pointing to the next.
What is the time to access the nth element in each?Basic
O(1) in an array and O(n) in a linked list, because the list must be walked from the head.
What is a node?Basic
One element of a linked list. It holds a value and a reference to the next node (and to the previous one in a doubly linked list).
Is a Python list a linked list?Basic
No. It is a dynamic array that stores references to objects, so indexing is O(1) and inserting at the front is O(n).
What is the cost of inserting in the middle of each?Intermediate
Array: O(n) because later items shift. Linked list: O(1) once you hold the node before the position, but O(n) to find that node first.
What is the difference between singly and doubly linked lists?Intermediate
A singly linked list has only next pointers, so it moves one way. A doubly linked list also has previous pointers, which allows moving both ways and easier deletion at the cost of more memory.
Why can arrays be faster even for operations with the same Big-O?Intermediate
Contiguous memory is cache friendly, so the processor reads many items at once. Linked nodes are scattered, causing cache misses and extra pointer overhead.
How do you reverse a singly linked list?Intermediate
Walk the list while keeping previous, current and next pointers. At each node point current.next to previous, then move all three forward. It takes O(n) time and O(1) space.
How would you detect a cycle in a linked list?Advanced
Use Floyd's tortoise and hare: move one pointer one step and another two steps. If they ever meet there is a cycle; if the fast one reaches the end there is none. It takes O(n) time and O(1) space.
How does a dynamic array grow, and why is append amortised O(1)?Advanced
When full it allocates a larger block, often about double, and copies items over. Copies are rare, so spread across many appends the average cost per append is constant.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.