# Arrays vs linked lists

> Arrays give indexed access in contiguous memory; linked lists pay pointer hops for cheap middle inserts.

Source: https://fullbytedeveloper.com/blog/arrays-vs-linked-lists
Published here: 2026-12-03, updated 2026-12-03

An array stores same-size elements so an index computes a memory address [1]. A linked list stores nodes that point to the next node, so order is not physical placement in memory [2]. That single difference drives most interview tradeoffs. Related: [how a CPU runs an instruction](/blog/how-a-cpu-runs-an-instruction), [stack vs heap memory](/blog/stack-vs-heap-memory).

## Quick start (under a minute)

If you can jump to item `k` in one step with `base + k * size`, you are looking at array semantics [1]. If you must start at the head and follow `next` pointers until you reach the k-th node, you are looking at linked-list semantics [2].

Default to the contiguous form for read-heavy code. Reach for pointer chains only when your measured workload does frequent front inserts, interior deletes during iteration, or other patterns Oracle documents for `LinkedList` [3].

## Picture memory layout and cache lines

Wikipedia: arrays compute element addresses from indices; linked lists scatter nodes wherever the allocator places them and follow links [1] [2]. Linked-list articles also note weaker spatial locality than arrays because nodes are not guaranteed to sit next to each other [2].

<figure aria-label="Contiguous array index math beside linked nodes requiring pointer hops">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 220" role="img" aria-hidden="true">
  <rect x="0" y="0" width="360" height="220" rx="12" fill="#0b1f3a"/>
  <text x="90" y="20" text-anchor="middle" font-size="12" font-weight="600" fill="#e6edf7" font-family="system-ui,sans-serif">Array [1]</text>
  <text x="270" y="20" text-anchor="middle" font-size="12" font-weight="600" fill="#e6edf7" font-family="system-ui,sans-serif">Linked list [2]</text>
  <line x1="180" y1="28" x2="180" y2="210" stroke="#14b8a6" stroke-width="0.642857" stroke-dasharray="4 4"/>
  <rect x="25.7143" y="35.5" width="28.2857" height="45" fill="#123052" stroke="#14b8a6" stroke-width="0.964286"/>
  <rect x="56.5714" y="35.5" width="28.2857" height="45" fill="#123052" stroke="#14b8a6" stroke-width="0.964286"/>
  <rect x="87.4286" y="35.5" width="28.2857" height="45" fill="#123052" stroke="#14b8a6" stroke-width="0.964286"/>
  <rect x="118.286" y="35.5" width="28.2857" height="45" fill="#123052" stroke="#14b8a6" stroke-width="0.964286"/>
  <text x="39.8571" y="63" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">A</text>
  <text x="70.7143" y="63" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">B</text>
  <text x="101.571" y="63" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">C</text>
  <text x="132.429" y="63" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">D</text>
  <text x="90" y="98" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">index 2 → one address calc</text>
  <circle cx="205.714" cy="62" r="22" fill="none" stroke="#14b8a6" stroke-width="0.964286"/>
  <text x="205.714" y="66" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">A</text>
  <circle cx="257.143" cy="88" r="22" fill="none" stroke="#14b8a6" stroke-width="0.964286"/>
  <text x="257.143" y="92" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">B</text>
  <circle cx="295.714" cy="52" r="22" fill="none" stroke="#14b8a6" stroke-width="0.964286"/>
  <text x="295.714" y="56" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">C</text>
  <line x1="217.286" y1="68" x2="245.571" y2="82" stroke="#14b8a6" stroke-width="0.771429"/>
  <line x1="268.714" y1="80" x2="284.143" y2="62" stroke="#14b8a6" stroke-width="0.771429"/>
  <text x="270" y="128" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">reach C: chase links</text>
  <text x="90" y="140" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">middle insert: shift tail [4]</text>
  <text x="270" y="156" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">middle insert: relink pointers</text>
  <text x="180" y="196" text-anchor="middle" font-size="12" fill="#e6edf7" font-family="system-ui,sans-serif">cache lines favor contiguous slots</text>
</svg>
</figure>

The diagram is about address math versus pointer chasing, not about every language's implementation detail. Dynamic arrays still resize with amortized cost; linked lists still pay an extra word per node for the link field [4]. The memorable production lesson: profile indexed reads before you switch collections for theoretical insert wins.

## Cost the operations you actually do

| Need | Array / ArrayList-style | Linked list |
|---|---|---|
| Read by index | Constant-time address math [1] [3] | Walk from the head [2] [3] |
| Insert/remove in the middle | Shift neighbors to keep contiguity [4] | Relink a few pointers once you are at the spot [2] [3] |
| Append at end (with spare capacity) | Usually cheap amortized [3] [4] | Cheap at tail if you keep a tail pointer [3] |
| Memory overhead | Slot size only (plus unused capacity) | Data plus pointer(s) per node [2] |

OpenDSA: average insert or remove in an array-based list moves about half the elements when inserting or deleting in the interior [4]. Oracle: `ArrayList` offers fast positional access; `LinkedList` helps for frequent front inserts or interior deletes during iteration, but positional access is linear and constant factors are worse, so measure before switching [3].

Oracle's tutorial also states plainly that most of the time you will use `ArrayList`, and that if you think you want `LinkedList` you should measure both implementations [3]. That guidance matches what you see on CPUs: fewer instructions for indexed loads, more predictable memory access [1].

## Run a micro contrast on this draft host

This demo exaggerates the linked-list walk in Python to teach pointer chasing. It is not a JVM `LinkedList` benchmark.

```python title="array_vs_walk.py"
import time

N = 100_000
arr = list(range(N))

def walk_middle(xs):
    target = len(xs) // 2
    i = 0
    while i < target:
        i += 1
    return i

t0 = time.perf_counter()
for _ in range(1000):
    _ = arr[N // 2]
t1 = time.perf_counter()

t2 = time.perf_counter()
for _ in range(1000):
    walk_middle(arr)
t3 = time.perf_counter()

print("array index 1000x:", round((t1 - t0) * 1000, 3), "ms")
print("walk-to-middle 1000x:", round((t3 - t2) * 1000, 3), "ms")
```

```bash
python3 array_vs_walk.py
```

Observed on this draft host (Python 3.12.x, Linux x86_64, 2026-12-03):

```text
array index 1000x: 0.077 ms
walk-to-middle 1000x: 795.111 ms
```

Teaching points from this run only: indexed access stayed sub-millisecond for 1000 reads at the middle of a 100000-element Python list. The simulated walk loop spent orders of magnitude more time because it cannot use index math. Absolute milliseconds change with CPU, interpreter build, and `N`. Do not quote these numbers as universal law.

## Tie the picture to memory and the CPU

Array indices assume fixed-size slots so the address calculation is addition and multiplication [1]. That lines up with how a load instruction uses a base register plus offset after the address is known ([how a CPU runs an instruction](/blog/how-a-cpu-runs-an-instruction)). Nodes in a linked list are usually heap objects with separate allocations, so each hop is a dependent load chain ([stack vs heap memory](/blog/stack-vs-heap-memory)).

Interviewers often list `O(1)` for array index and `O(n)` for linked-list index. Both statements are about operation counts, not wall time. A small `n` with terrible locality can still beat a large array on paper if the constants differ. Measure the hot path.

## When a linked list is still the right lecture

Sorted insertion into a linked structure can avoid shifting every tail element when OpenDSA discusses list variants [4]. Doubly linked lists add a `prev` pointer so deletes at a known node are pointer updates without scanning from the head [2]. Those cases matter for textbook completeness.

In day-to-day application code, language collections hide the concrete type. Java's `List` interface does not tell you which implementation backs it until you construct `ArrayList` or `LinkedList` [3]. Python's built-in `list` is a dynamic array even though the name says list. C++ `std::list` is a doubly linked list; `std::vector` is the array form. Read the type you instantiated, not the interface name alone.

## Pick a structure for your workload

Start with the dynamic array default Oracle recommends [3]. Switch only when profiling shows a hotspot in front inserts, iterator-based removals, or another pattern linked lists handle with fewer shifts [3] [4].

If you are choosing for a course assignment, follow the assignment spec. If you are choosing for production, log allocation rates and cache misses when you can. Big-O cards are a filter, not a finish line.

## Mistakes that waste a week

Treating every `List` type as a linked list. Python, Rust `Vec`, Go slices, and Java `ArrayList` are contiguous or array-backed. The word list in the API name is not evidence of pointer chains.

Quoting only Big-O on a whiteboard. An `O(n)` walk with tiny `n` can beat an `O(1)` index with locking and bounds checks elsewhere in the system. Bring numbers from a profiler or admit you have not measured.

Linked lists always win on insert. You still pay `O(n)` to find the insertion point unless you already hold a pointer to the node. Array inserts at the end with spare capacity stay cheap [4].

Ignoring cache because the exam said pointer insert is `O(1)`. After you locate the node, relinking is cheap. Locating the node often is not. Wikipedia's locality note exists for a reason [2].

Choosing linked lists to avoid resize copies. Dynamic arrays resize with amortized doubling strategies [4]. Linked lists trade resize bursts for per-node overhead and scattered memory.

Before you flip implementations in prod, log whether the hotspot is indexed reads or interior inserts. The Python contrast above is a teaching aid, not a JVM shootout. Your next experiment: run the script with your real list size `N`, note the ratio between index and walk timings, and only then argue linked lists in code review.

Send this to the friend who reaches for a linked list without a locality reason.

Image: Concept illustration of contiguous array slots with base-plus-index addressing beside scattered linked nodes with arrow chains, no people and no readable text.
https://fullbytedeveloper.com/media/arrays-vs-linked-lists/arrays-vs-linked-lists-venice.webp
Indexed arrays trade pointer hops for one address calculation.

## Sources

1. [Wikipedia: Array (data structure), accessed 2026-12-03](https://en.wikipedia.org/wiki/Array_(data_structure))
2. [Wikipedia: Linked list, accessed 2026-12-03](https://en.wikipedia.org/wiki/Linked_list)
3. [Oracle Java Tutorials: List Implementations, accessed 2026-12-03](https://docs.oracle.com/javase/tutorial/collections/implementations/list.html)
4. [OpenDSA CS2: Array-Based List Implementation, accessed 2026-12-03](https://opendsa-server.cs.vt.edu/ODSA/Books/CS2/html/ListArray.html)
5. [Full Byte Developer: How a CPU runs an instruction](https://fullbytedeveloper.com/blog/how-a-cpu-runs-an-instruction)
6. [Full Byte Developer: Stack vs heap memory](https://fullbytedeveloper.com/blog/stack-vs-heap-memory)
