All CS Videos
DSABeginner friendly

Arrays vs linked lists: when to use each

Compare memory layout, access, insertion, deletion, cache behavior, and practical use cases for arrays and linked lists.

Explore course
Read by 50K+ learners
Written byJeevesh Rai
01 Sep 20268 min read
Visual comparison of arrays and linked lists
A visual guide from the DSA with C++ course.

The physical difference

Contiguous values versus connected nodes

An array keeps elements next to one another, which makes indexed access fast and works well with CPU caches. A linked list stores separate nodes connected by references.

Linked nodes can live in different memory locations, so reaching the tenth node requires following the first nine links.

Practical defaultUse a dynamic array unless your workload genuinely benefits from linked-node operations.
Visual comparison of arrays and linked lists
The concept at a glance. The sections below turn each part into a practical decision.

Complexity with context

Insertion cost depends on what you already know

Inserting near the start of an array shifts later elements. Inserting after a known linked-list node changes a few references. But finding that node may still require a linear traversal.

Linked lists also use extra memory for references and usually have poorer cache locality.

  • Array index access: O(1)
  • Linked-list index access: O(n)
  • Array append: amortized O(1)
  • Known-node linked insertion: O(1)

See the pointer change

Insert without losing the rest of the list

Save the old next reference before connecting the previous node to the new node. Drawing the links first prevents accidental disconnection.

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

def insert_after(node, value):
    node.next = Node(value, node.next)

Continue learning

Build the complete mental model with the visual lessons, examples, and practice material in the DSA with C++ course.

Explore the course