AlgoScope

Array vs Singly Linked List

Data StructuresData structurebeginner

Array and Singly Linked List are both data structures. Play them side by side below, then see exactly where they diverge.

Either player can stop before each decision and ask what happens next: turn on Predict mode in its top bar.

Numbered slots side by side, so reaching any one of them costs the same.

Shift: 8 moves from index 4 to the empty slot at index 5.501142234856i
Move 8 from index 4 to index 5.

INDEX -> CELL -> VALUE. Insert shifts neighbours right, delete shifts left.

Singly Linked List

Full topic

Each node points at the next. To reach the tenth you walk the first nine.

Linked list 5, 1, 4, 2. Reversing in place.5142currhead
Reverse it in place by walking with prev, curr and next, flipping one arrow per node.

VALUE | NEXT boxes joined by arrows. Insertion is a pointer detaching and reattaching, never a shift.

How they differ

An array keeps its values in numbered slots side by side, so reaching any one is O(1), but inserting or deleting in the middle shifts everything after it: O(n). A singly linked list reaches the tenth node only by walking the first nine, O(n), but once you hold a node, inserting or deleting after it is O(1) - nothing moves, one pointer changes.

The run shown here takes 6 steps for Array and 11 steps for Singly Linked List. That count moves with the input - use the example tabs above either player to see it change.

What each operation costs, for Array and Singly Linked List side by side.
CostArraySingly Linked List
accessO(1)O(n)
searchO(n)O(n)
insertO(n)not documented
deleteO(n)not documented
insert at headnot documentedO(1)
insert after nodenot documentedO(1)
delete after nodenot documentedO(1)
insert at tailnot documentedO(n)
spaceO(n)O(n)

A dash means the curriculum entry for that topic documents no cost under that name. It is not a claim that the operation is impossible: push on a stack and enqueue on a queue are written as different operations, so they are listed as different rows rather than paired up here.