Back to List

Array vs Linked List

Understand how arrays and linked lists differ in memory and when to use which.

Beginner
|
7min
|
Verified (2026-07)
Progress0/23 (0%)

Array vs. Linked List

After this topic

You will be able to explain the difference in memory structure between arrays and linked lists, and determine which one to choose depending on the situation.


Array – Contiguous Memory

An array stores data contiguously in memory.

text
Memory Address:  100  104  108  112  116
                +----+----+----+----+----+
Value:         | 10 | 20 | 30 | 40 | 50 |
                +----+----+----+----+----+
Index:         0    1    2    3    4

The advantage is immediate access by index.

python
arr = [10, 20, 30, 40, 50]
print(arr[3]) # 40 β€” immediate access (O(1))

If you say "the 3rd slot," it's simply the starting address + (3 Γ— size) = directly to that location. No matter how many elements, it finds it at once.

Array's Weakness: Insertion and Deletion

text
To insert 15 at index 1 in [10, 20, 30, 40, 50]?

Step 1: Shift 20, 30, 40, and 50 one slot to the right.
Step 2: Place 15 in the empty slot.

[10, 15, 20, 30, 40, 50]

If there are 1 million data points, inserting at the very beginning requires shifting all 1 million data points.


Linked List – Scattered Memory

A linked list stores each data point and remembers the location of the next data point.

text
[10|β†’] β†’ [20|β†’] β†’ [30|β†’] β†’ [40|β†’] β†’ [50|βˆ…]

Each node = value + address of the next node (pointer)

It doesn't need to be stored contiguously in memory. Each node just needs to know "what's next."

Linked List's Advantage: Insertion and Deletion

text
To insert 25 after 20 in [10|β†’] β†’ [20|β†’] β†’ [30|β†’]:

Step 1: Create a new node [25|β†’].
Step 2: Change the pointer of 20 to 25, and the pointer of 25 to 30.

[10|β†’] β†’ [20|β†’] β†’ [25|β†’] β†’ [30|β†’]

No need to move other nodes. Just change two pointers. O(1).

Linked List's Weakness: Access

python
# "What is the 3rd value?"
# Array: arr[3] β†’ immediate (O(1))
# Linked list: must follow 1β†’2β†’3 from the beginning (O(n))

Since there is no index, finding the nth value requires following from the beginning n times.


Comparison Summary

OperationArrayLinked List
Index AccessO(1) ImmediateO(n) Requires traversal
SearchO(n) TraversalO(n) Traversal
Insert at BeginningO(n) Shift all elementsO(1) Change pointer
Insert at EndO(1) Add to the endO(1) if tail pointer exists
Insert in MiddleO(n) ShiftingO(1) Change pointer
MemoryRequires contiguous memoryCan be scattered

Selection Criteria

SituationRecommendation
Frequent access by indexArray
Frequent insertions/deletionsLinked list
Size changes frequentlyLinked list
Memory efficiency is importantArray (no pointer overhead)
Cache friendliness is neededArray (contiguous memory)

In practice, most of the time we use arrays (Python's list, JavaScript's Array). Modern languages' dynamic arrays automatically adjust the size and are beneficial for CPU caching. Linked lists are used in special cases (queue, stack implementation, large-scale insertions/deletions).


πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...