Array vs Linked List: Differences, Complexity and When to Use Each
Compare arrays and linked lists on access, insertion, memory and cache behaviour, then choose the right structure for common programming scenarios.
Array vs linked list comes down to access against insertion. An array stores elements side by side, so reading by index is O(1), but inserting in the middle shifts elements and costs O(n). A linked list connects nodes with pointers, so inserting at a node you hold is O(1), but reaching the k-th element is O(n). In practice, arrays win more often.
Key takeaways
- Arrays give O(1) index access, linked lists give O(1) insertion at a node you already hold.
- Cache locality makes arrays faster in practice than big-O alone suggests.
- Python lists and Java ArrayList are dynamic arrays, and Python has no built-in linked list.
- Default to a dynamic array. Pick a linked list for pointer-level inserts, such as an LRU cache.
On this page:
- How do arrays and linked lists store data?
- How do the operations compare one by one?
- What are the memory and performance trade-offs?
- Which structure should you choose in common scenarios?
- Next step
- Frequently asked questions
| Criterion | Array / dynamic array | Linked list |
|---|---|---|
| Memory layout | Contiguous block | Scattered nodes joined by pointers |
| Access by index | O(1) | O(n) |
| Search (unsorted) | O(n) | O(n) |
| Insert or delete at the start | O(n) | O(1) |
| Insert or delete at the end | O(1) amortised (dynamic array) | O(1) with a tail pointer; delete is O(n) in a singly list |
| Insert or delete in the middle | O(n) | O(1) once you hold the node, O(n) to find it |
| Extra memory | Spare capacity in dynamic arrays | One or two pointers per element |
| Cache behaviour | Good, neighbours load together | Poor, each hop can miss the cache |
| Size | Fixed, or grows by resizing | Grows one node at a time |
How do arrays and linked lists store data?
An array reserves one contiguous block. A linked list allocates each node separately and links them with pointers.
Because array elements are the same size and adjacent, the address of element i is just base + i * size. That one calculation is why indexing is O(1). A linked list has no such formula, so you walk from the head.
arr = [10, 20, 30] # one block: 10 | 20 | 30
list: 10 -> 20 -> 30 -> None # three nodes, anywhere in memoryPython's built-in list and Java's ArrayList are dynamic arrays. They resize by allocating a bigger block and copying, which is why appending is amortised O(1). For each structure alone, see our guides on arrays and linked lists.
How do the operations compare one by one?
Run the same task on both and the difference shows up. Task: insert a value at the front of a collection holding n items.
from collections import deque
nums = [1, 2, 3]
nums.insert(0, 0) # array: shifts every element, O(n)
dq = deque([1, 2, 3])
dq.appendleft(0) # O(1), no shiftingPython has no built-in linked list. A deque gives you cheap operations at both ends, and for most programs that is the practical answer when you think you want a linked list.
Now the reverse task: read the 500th element. The array does one address calculation. The linked list follows 499 pointers. If your code reads by position often, the array wins by a wide margin.
What are the memory and performance trade-offs?
Linked lists cost more memory per element and run slower on modern CPUs than their big-O suggests. Both effects come from pointers and cache behaviour.
Illustrative estimate for 1,000 four-byte integers on a typical 64-bit C program: the array needs about 4,000 bytes. A node with a 4-byte value and an 8-byte pointer is usually padded to 16 bytes, so the list needs about 16,000 bytes, plus allocator overhead. Exact numbers vary by language and runtime.
Cache locality adds to this. The CPU loads memory in blocks, so an array scan gets neighbouring elements nearly free. A linked list node can sit anywhere, so each step may wait on main memory. Two structures with the same big-O can therefore differ a lot in real time.
Arrays have a cost too. Resizing a full dynamic array copies everything once, and a fixed array cannot grow at all. If you must hold references to elements while the collection grows, a resize can invalidate them in languages with raw pointers.
Which structure should you choose in common scenarios?
Default to a dynamic array. Choose a linked list only when pointer-level insertion or removal is the main workload.
- Store marks for 60 students and read by roll number: array. Index access is the whole job.
- Queue of print jobs with frequent joins and leaves at the ends: deque or linked list. See stacks and queues.
- LRU cache that moves a used item to the front in O(1): doubly linked list plus a hash map. See hash maps.
- Sorting or binary search: array, because both need random access. See binary search with code.
- Undo history that grows and shrinks at one end: either works, and a dynamic array used as a stack is simpler.
In Java, LinkedList is rarely faster than ArrayList in real programs, even for inserts, unless you insert through an iterator at a known position. Benchmark your own workload before switching.
Next step
Choosing the right structure is the core of DSA interviews and of system design later. If you want a guided route from programming fundamentals into AI and machine learning, see the Artificial Intelligence and Machine Learning programme from Vishlesan i-Hub, IIT Patna and check the programme page for the current batch date and eligibility.
Frequently asked questions
What is the main difference between an array and a linked list?
An array keeps elements in one contiguous block and gives O(1) index access. A linked list keeps separate nodes joined by pointers and gives O(1) insertion at a known node but O(n) access by position.
Which is faster, an array or a linked list?
For reading and scanning, arrays are usually faster because of O(1) indexing and cache-friendly memory. Linked lists only win when you repeatedly insert or delete at positions you already hold.
When should I use a linked list instead of an array?
Use one when insertions and deletions at known nodes dominate and you rarely need random access, for example in an LRU cache built with a doubly linked list and a hash map.
What is the difference between ArrayList and LinkedList in Java?
ArrayList is a dynamic array with O(1) get by index. LinkedList is a doubly linked list with O(n) get. For most workloads ArrayList is the better default, so measure before choosing LinkedList.
Is a Python list an array or a linked list?
A Python list is a dynamic array of references. Indexing is O(1) and inserting at the front is O(n). For O(1) operations at both ends, use collections.deque.
Do linked lists use more memory than arrays?
Usually yes. Each node stores a pointer or two besides the value, and nodes carry allocator overhead. Dynamic arrays also waste some spare capacity, but the per-element cost is typically lower.