Why are linked lists faster than arrays? Why are linked lists faster than arrays? arrays arrays

Why are linked lists faster than arrays?


This question title is misleading.

It asserts that linked lists are faster than arrays without limiting the scope well. There are a number of times when arrays can be significantly faster and there are a number of times when a linked list can be significantly faster: the particular case of linked lists "being faster" does not appear to be supported.

There are two things to consider:

  1. The theoretical bounds of linked-lists vs. arrays in a particular operation; and
  2. the real-world implementation and usage pattern including cache-locality and allocations.

As far as the access of an indexed element: The operation is O(1) in an array and as pointed out, is very fast (just an offset). The operation is O(k) in a linked list (where k is the index and may always be << n, depending) but if the linked list is already being traversed then this is O(1) per step which is "the same" as an array. If an array traversal (for(i=0;i<len;i++) is faster (or slower) depends upon particular implementation/language/run-time.

However, if there is a specific case where the array is not faster for either of the above operations (seek or traversal), it would be interesting to see to be dissected in more detail. (I am sure it is possible to find a language with a very degenerate implementation of arrays over lists cough Haskell cough)

Happy coding.


My simple usage summary: Arrays are good for indexed access and operations which involve swapping elements. The non-amortized re-size operation and extra slack (if required), however, may be rather costly. Linked lists amortize the re-sizing (and trade slack for a "pointer" per-cell) and can often excel at operations like "chopping out or inserting a bunch of elements". In the end they are different data-structures and should be treated as such.


Like most problems in programming, context is everything. You need to think about the expected access patterns of your data, and then design your storage system appropriately. If you insert something once, and then access it 1,000,000 times, then who cares what the insert cost is? On the other hand, if you insert/delete as often as you read, then those costs drive the decision.


Depends on which operation you are referring to. Adding or removing elements is a lot faster in a linked list than in an array.

Iterating sequentially over the list one by one is more or less the same speed in a linked list and an array.

Getting one specific element in the middle is a lot faster in an array.

And the array might waste space, because very often when expanding the array, more elements are allocated than needed at that point in time (think ArrayList in Java).

So you need to choose your data structure depending on what you want to do:

many insertions and iterating sequentially --> use a LinkedList

random access and ideally a predefined size --> use an array