Array vs Linked List: Access, Insertion, Cache and Complexity
An array stores elements of the same type next to each other in memory with no gaps, so the position of any element can be computed from its index and read or written in constant time. A linked list is a data structure in which each element holds a value and a pointer to the next element, and the elements are linked by following those pointers, so they can sit in separate places in memory. An array reads fast, but inserting or deleting in the middle means shifting the elements behind it. A linked list can insert or delete just by changing pointers once the position is known, but reading the i-th element means walking from the head. The basic rule: use an array when you mostly read by index, and a linked list when you often insert or delete at known positions.
| Aspect | Array | Linked list |
|---|---|---|
| Memory layout | Elements sit next to each other in one contiguous block | Elements are scattered and linked by pointers |
| Reading the i-th element | O(1): the address is start address + i × element size | O(n): follow the pointers i times from the head |
| Searching for a value | O(n) linear search if unsorted; O(log n) with binary search if sorted | O(n) even if sorted; binary search cannot be used because there is no way to jump to the middle |
| Inserting or deleting in the middle | O(n): the elements behind it are shifted one by one | O(1) if the position (the preceding element) is known; only pointers change |
| Appending at the end | O(1) if there is free space; a dynamic array that doubles its capacity occasionally copies everything but is O(1) on average (amortized) | O(1) if a pointer to the last element is kept |
| Resizing | A fixed-size array's size is set when it is created; growing it means allocating a new block and copying | Space is allocated per element, so it grows only as much as needed |
| Memory use | Only the elements themselves (a dynamic array keeps some spare capacity) | Extra space for a pointer per element (two in a doubly linked list) |
| Cache friendliness | Good: elements are contiguous, so sequential reads make good use of caches and prefetching | Poor: elements tend to be scattered, so even the same O(n) traversal can be much slower than with an array |
| Typical uses | Tables read and written by index, matrices and images, data to sort or binary-search, implementations of stacks and queues | Sequences with frequent insertion and deletion, chaining in hash tables, free-space management |
Figures are typical orders of magnitude and vary by process generation and product.