Curious Minds

Clear answers to science and history questions

Why Linked Lists Solve Problems Arrays Cannot

· 7 min read · Science

Linked lists solve problems that arrays cannot because they allow efficient insertion and deletion of elements anywhere in the sequence without shifting other elements, and they can grow or shrink dynamically without reallocating a contiguous block of memory. Arrays, by contrast, store elements in contiguous memory, so inserting or deleting in the middle requires moving many elements, and resizing often means copying the entire array. Linked lists trade these costs for different ones—slower random access and extra memory for pointers—but for many problems that trade-off is exactly what is needed.

Background: How Arrays and Linked Lists Store Data

An array is a block of memory divided into equal-sized slots, one per element. Because the slots are contiguous, the address of any element can be computed directly from its index: base address plus index times element size. That makes reading or writing the element at position i a constant-time operation, O(1). The cost is that the block must be allocated at once, and its size is fixed at allocation (or requires reallocation to change).

A linked list instead stores each element in a separate node. Each node contains the data and one or more references (pointers) to other nodes. In a singly linked list, each node points to the next; in a doubly linked list, each node points to both the next and the previous node. The nodes need not be contiguous in memory. The list is accessed through a head pointer (and sometimes a tail pointer), and traversing it means following the pointers from node to node.

This structural difference is the root of every advantage and disadvantage. The linked list gives up direct indexing in exchange for flexibility in where nodes live and how the sequence can be rearranged.

Insertion and Deletion Without Shifting

The most important problem linked lists solve is efficient insertion and deletion at arbitrary positions. In an array, inserting an element at index k requires shifting every element from k to the end one slot to the right; deleting at k requires shifting everything after it one slot left. On average, that is about half the elements moved, so the operation is O(n) in the number of elements.

In a linked list, if you already have a pointer to the node before the insertion point, inserting a new node is a constant-time operation: create the node, set its next pointer to the old next node, and set the previous node's next pointer to the new node. Deletion is similarly constant-time: adjust the previous node's pointer to skip the removed node. No other elements move. The catch is that finding the position in the first place is O(n) unless you already hold a reference to the relevant node.

This matters in practice whenever a program maintains a sequence that changes frequently in the middle. Examples include:

  • A text editor's internal buffer, where characters are inserted and deleted at the cursor.
  • An LRU (least recently used) cache, where items are moved to the front on access and removed from the back when full.
  • Undo/redo histories, where operations are added and removed at the current position.
  • Adjacency lists in graph representations, where edges are added as the graph is built.

In each case, the linked list avoids the repeated shifting that an array would require.

Dynamic Size and Memory Flexibility

Arrays generally require a contiguous block of memory of a fixed size. If the program needs more elements than the array can hold, it must allocate a larger block and copy all existing elements into it—an O(n) operation. Some languages provide dynamic arrays (such as Python's list or Java's ArrayList) that handle this automatically by over-allocating and doubling capacity when needed, but the underlying copy still happens occasionally, and the array still needs a contiguous region.

Linked lists grow and shrink one node at a time. Adding an element means allocating a node and linking it in; removing one means unlinking it and freeing the memory. There is no need to copy existing elements, and no need for a large contiguous block. This is valuable when:

  • The total number of elements is unknown in advance and changes frequently.
  • Memory is fragmented, so a large contiguous block is unavailable but many small blocks are.
  • The program must avoid pauses caused by occasional large reallocations (though garbage collection and allocator behavior can introduce their own pauses).

It is worth noting that dynamic arrays often outperform linked lists in practice for many workloads because of cache locality and lower per-element overhead. The linked list's advantage is structural, not universal.

Splicing, Merging, and Persistent Structures

Linked lists make certain structural operations cheap that are expensive or awkward with arrays. Concatenating two linked lists, for example, can be done in constant time if you have pointers to the tail of the first and the head of the second: just link the tail to the head. With arrays, concatenation requires copying all elements into a new block.

Splicing a sublist out of one list and into another is also a pointer manipulation in a linked list, whereas an array requires shifting. This property is exploited in functional programming, where persistent (immutable) linked lists allow sharing of structure: adding an element to the front creates a new list that shares the entire tail with the old list, using O(1) extra memory. Arrays cannot share structure this way without copying, because any change to an element affects the whole block.

These operations underpin algorithms such as:

  • Merge sort on linked lists, which can be implemented with O(1) extra space by relinking nodes rather than copying data.
  • Free-list management in memory allocators, where free blocks are kept in a linked list and allocated or returned by pointer updates.
  • Hash table collision chains, where each bucket holds a linked list of entries and insertion/removal is constant-time given the bucket.

When Arrays Are Still the Better Choice

Linked lists are not a universal improvement. Their main costs are:

  • No random access: reaching the i-th element requires walking i nodes, O(n) time. Arrays give O(1) access.
  • Extra memory: each node stores at least one pointer (often 8 bytes on 64-bit systems), plus allocator overhead per allocation. For small data items, this can double or triple memory use.
  • Poor cache locality: nodes are scattered in memory, so traversal causes many cache misses. Arrays, being contiguous, benefit from prefetching and spatial locality.
  • Allocation overhead: creating and freeing nodes is more expensive than indexing into a preallocated block.

As a rule of thumb, arrays (especially dynamic arrays) are better when the workload is dominated by random access, iteration, or appending at the end. Linked lists are better when the workload involves frequent insertion and deletion in the middle, splicing, or structural sharing. Many real systems use hybrid approaches: a dynamic array for storage plus a linked list or index for ordering, or an unrolled linked list that stores several elements per node to improve locality.

Common Problems and Pitfalls

Linked lists introduce failure modes that arrays do not have. The most common are:

  • Lost nodes and memory leaks: if a pointer is overwritten without freeing the node it pointed to, that node becomes unreachable and its memory is leaked (in languages without garbage collection).
  • Dangling pointers: if a node is freed while another pointer still refers to it, later access is undefined behavior.
  • Cycles: a bug in pointer manipulation can create a loop, causing traversal to never terminate.
  • Off-by-one errors at the head or tail: inserting or deleting at the boundaries requires special handling of the head (and tail) pointers.
  • Inefficient search: searching a linked list is O(n) and cannot be sped up by binary search because there is no random access.

These are manageable with careful coding and, in modern languages, with garbage collection and safe references, but they explain why linked lists are often avoided in application code unless their structural advantages are clearly needed.

Frequently Asked Questions

Can a linked list do anything an array cannot?

In terms of computability, no—both can represent any finite sequence. The difference is efficiency: linked lists make some operations asymptotically cheaper (insertion/deletion given a node reference, splicing, structural sharing) while making others more expensive (random access).

Why not always use a linked list?

Because the costs—O(n) access, extra memory per node, poor cache locality, and allocation overhead—often outweigh the benefits. For workloads dominated by reading and iterating, arrays are usually faster in practice.

Are linked lists still used in modern software?

Yes, in specific roles: hash table chaining, free lists in allocators, LRU caches, adjacency lists, and functional data structures. They are less common as general-purpose sequence containers, where dynamic arrays have largely taken over.

What is the difference between a singly and doubly linked list?

A singly linked list has one pointer per node (to the next), so traversal is one-way and deletion requires the previous node. A doubly linked list has two pointers (next and previous), allowing bidirectional traversal and O(1) deletion given a node reference, at the cost of extra memory per node.

Can linked lists be sorted efficiently?

Yes. Merge sort is well suited to linked lists and runs in O(n log n) time with O(1) extra space (excluding recursion stack) by relinking nodes. Quicksort is less natural because it relies on random access for partitioning.

Do linked lists have better worst-case performance than dynamic arrays?

For insertion at a known position, yes: O(1) versus amortized O(1) for appending and O(n) for middle insertion. For access and iteration, no: O(n) versus O(1) and O(n) with much better constants. Worst-case guarantees depend on the operation, not on one structure being universally superior.

In this article
  1. Background: How Arrays and Linked Lists Store Data
  2. Insertion and Deletion Without Shifting
  3. Dynamic Size and Memory Flexibility
  4. Splicing, Merging, and Persistent Structures
  5. When Arrays Are Still the Better Choice
  6. Common Problems and Pitfalls
  7. Frequently Asked Questions