Blog

Linked Lists vs. Arrays: When to Use Each Data Structure

Use an array (a Python list, a Java ArrayList, a JavaScript array) by default. Pick a linked list only when you constantly insert or remove items at positions you already hold a reference to, and you almost never need to jump to the n-th item. In practice that is rare, but it is exactly what interviewers probe, so it is worth understanding why.

How each one is laid out in memory

An array stores its elements next to each other in one block of memory. Because every slot has the same size, the address of element i is simply start + i * size, so reading it takes constant time.

A linked list stores each element in its own node, and each node holds a pointer to the next one (and, in a doubly linked list, to the previous one). Nodes can live anywhere in memory. To reach the n-th element you have to follow n pointers from the head.

A note on terminology: a Python list is a dynamic array. It holds a contiguous block of references to objects and grows when it fills up. It is not a linked list, despite the name.

Time complexity side by side

Operation Array (dynamic) Singly linked list Doubly linked list with head and tail
Read element at index i O(1) O(n) O(n)
Search for a value O(n) O(n) O(n)
Insert at the front O(n), everything shifts O(1) O(1)
Insert at the end O(1) amortized O(1) with a tail pointer, O(n) without O(1)
Remove from the front O(n) O(1) O(1)
Remove from the end O(1) O(n), you need the previous node O(1)
Insert or remove next to a node you already hold O(n) O(1) for insert after, O(n) for remove (you need the previous node) O(1)
Insert or remove in the middle by index O(n) O(n), walking to the spot O(n), walking to the spot

Two details trip people up. First, “O(1) insertion in a linked list” assumes you already have the node. Finding the node is the O(n) part, and the walk is usually the dominant cost. Second, “O(1) amortized” for an array append means that an occasional append triggers a resize that copies everything, but averaged over many appends the cost per append is constant.

A singly linked list in Python

Here is a small implementation with a tail pointer, so appending is O(1). It also shows why indexing is slow: get has to walk the chain.

class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

class LinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def push_front(self, value):
        self.head = Node(value, self.head)
        if self.tail is None:
            self.tail = self.head

    def push_back(self, value):
        node = Node(value)
        if self.tail is None:
            self.head = self.tail = node
        else:
            self.tail.next = node
            self.tail = node

    def insert_after(self, node, value):
        node.next = Node(value, node.next)
        if node is self.tail:
            self.tail = node.next

    def get(self, index):
        node = self.head
        for _ in range(index):
            node = node.next
        return node.value

    def to_list(self):
        out, node = [], self.head
        while node:
            out.append(node.value)
            node = node.next
        return out

ll = LinkedList()
for x in [2, 3, 4]:
    ll.push_back(x)
ll.push_front(1)
ll.insert_after(ll.head.next, 99)
print(ll.to_list())   # [1, 2, 99, 3, 4]
print(ll.get(2))      # 99

This prints [1, 2, 99, 3, 4] and 99. insert_after touches two pointers and never moves any other element. The equivalent list.insert(1, 99) on a Python list has to shift every element after position 1 one slot to the right.

If you want to practice building and manipulating these, the Linked Lists lesson walks through it step by step, and Reverse Linked List is the classic first exercise.

Why arrays usually win in practice

Big O hides a constant factor that is often large: cache locality. When the CPU reads one array element it pulls a whole cache line (commonly 64 bytes) into its cache, so the next several elements are already there. Scanning an array is mostly cache hits. Scanning a linked list follows pointers to nodes scattered around memory, which tends to cause a cache miss at nearly every step. On top of that, each node carries its own pointer (and, in many languages, object header overhead), so a list of small items uses noticeably more memory than an array of the same items.

The consequence is that a plain array often beats a linked list even on operations where the list looks better on paper, such as inserting in the middle of a few thousand elements. The shifting is a fast, sequential memory copy, while the list pays for a slow pointer walk. You can check this on your own machine, since the exact crossover depends on the language and hardware. The point to remember is that “insertion is O(1) in a linked list” is a statement about the number of operations, not about speed.

The complexity gap is still real when the lists get large. Timing it in Python with 100,000 elements, 2,000 front insertions were slower on the list than on the linked list above, while reading the middle element was much faster on the list than walking the linked list. If your workload is mostly front insertions or removals, that is the signal to look at a deque or linked list rather than a plain array.

When a linked list is the right choice

  • Queues and deques with heavy front removal. Removing from the front of an array is O(n). A linked list does it in O(1). In Python you do not need to write one yourself: collections.deque gives O(1) at both ends. Java has ArrayDeque and LinkedList for the same job, and ArrayDeque is normally the faster of the two.
  • LRU caches. The standard design pairs a hash map (key to node) with a doubly linked list ordered by recency. The map finds a node in O(1), and because you hold the node, the list can unlink it and move it to the front in O(1). This is the textbook case where “insert and remove at a node you already hold” matters.
  • Hash tables with chaining. Each bucket can hold a short linked list of colliding entries. Many real implementations use arrays or trees for buckets instead, but chaining is the version you are asked to explain.
  • Stable references to elements. Appending to an array may reallocate and move elements, which invalidates pointers into it in languages like C and C++. Nodes of a linked list never move, so a pointer to a node stays valid until you delete that node.
  • Splicing. Joining two lists, or cutting one in two at a known node, is O(1) with pointers and O(n) with arrays.
  • Memory-constrained, allocation-by-piece situations, such as free lists in allocators, where the nodes are carved out of memory that is already free.

When an array is the right choice

  • You index by position, or do binary search. Binary search needs O(1) random access, which a linked list cannot provide.
  • You iterate over everything often. Sequential scans are where cache locality pays off.
  • You sort. Most standard-library sorts work on arrays.
  • You mostly append and read, which is the common case in application code.
  • The data is numeric or grid-like: matrices, images, time series, DP tables.

Common mistakes

  • Claiming linked lists “save memory” because they have no spare capacity. A dynamic array wastes some unused slots after growth, but a linked list pays a pointer (or two) per element, which is usually more.
  • Forgetting that inserting in the middle by index is O(n) for both. The list wins only if you already hold the node.
  • Using list.pop(0) or list.insert(0, x) in a loop in Python. Each call is O(n), so the loop is O(n^2). Use collections.deque when you need fast operations at the front.
  • Losing the head in linked list code. Insertions and deletions at the head are a special case. A dummy node before the head removes most of the special-casing.
  • Off-by-one on the tail pointer. If you keep a tail pointer, update it when you delete the last node or insert after it, as insert_after does above.

How to answer this in an interview

State the access pattern first: “What operations dominate, and do I have a handle to the node?” Then give the trade-off in one breath: arrays give O(1) indexing and great cache behavior, linked lists give O(1) insert and remove at a known node but O(n) access and extra memory per element. If the problem is LRU cache, queue, or in-place list manipulation, a linked list is expected. Otherwise, say you would start with an array and move to something else only if measurements show a problem.

For more on how linked lists appear in interview problems, see fast and slow pointers and in-place reversal of a linked list. For the complexity notation used here, time complexity explained covers the basics.

A lesson in AlgoCademy: the step to write, the line typed in the editor and checked, Step 3 passed; then a typo, the error pointing at its line, the fix, and Step 4 passed.

Before you go

Start thinking like a software engineer.

Learn to code and solve interview problems, one small, checked step at a time.

Try Reverse Linked List free

No account needed. You write real code in your browser.