Full Byte Developer.

Arrays vs linked lists

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

Published here , updated · 6 sources

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, 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].

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

NeedArray / ArrayList-styleLinked list
Read by indexConstant-time address math [1] [3]Walk from the head [2] [3]
Insert/remove in the middleShift 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 overheadSlot 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.

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). Nodes in a linked list are usually heap objects with separate allocations, so each hop is a dependent load chain (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.

Concept illustration of contiguous array slots with base-plus-index addressing beside scattered linked nodes with arrow chains, no people and no readable text.
Indexed arrays trade pointer hops for one address calculation.

Keep this guide

By Full Byte Developer

Filed under Science, Dev explainers

Questions about this guide

Why are arrays usually faster for random reads?

Array elements sit in contiguous memory, so an index maps to an address with base plus offset math. Linked lists need pointer chasing from the head, so worst-case access grows with position. Wikipedia and Oracle both call random access constant time for array-backed lists versus linear for linked implementations.

When does a linked list win?

Oracle's List tutorial: prefer LinkedList when you often add at the front or delete while iterating the interior. OpenDSA adds that inserting into a sorted linked structure can avoid shifting the whole tail. Measure anyway; ArrayList often wins on modern CPUs because of cache locality.

Why do inserts in the middle of an array cost more?

OpenDSA's array-list notes: keeping elements contiguous means shifting neighbors on insert or remove, about half the list on average for a uniform random position.

Is a language List always a linked list?

No. Python list, Java ArrayList, and C++ vector are dynamic arrays. Check the concrete type before you argue Big-O in an interview.

Does Big-O alone pick the winner?

No. Constant factors, allocator overhead, and cache lines often favor arrays for read-heavy workloads. Profile the operations you actually do.

Sources

Next in this stack