DocsHub
Linked Lists

Linked Lists — Introduction

Learn what linked lists are, how they differ from arrays, and when to use them.

Linked Lists

A linked list is a sequence of nodes where each node stores a value and a pointer to the next node. Unlike an array, the nodes are not stored in contiguous memory — they can be scattered anywhere, connected only by their pointers.

Think of a linked list like a treasure hunt. Each clue tells you where the next clue is. You cannot jump directly to clue 5 — you have to follow the chain from clue 1.


How it looks in memory

Array:
[10][20][30][40][50]   ← contiguous, each element right next to the next

Linked List:
[10|→] ··· [20|→] ··· [30|→] ··· [40|→] ··· [50|∅]
  ↑                                              ↑
 head                                           tail

Each node contains two things — the data and a pointer (next) to the next node. The last node points to None.

Linked Listinteractive · 5 nodes
head
value
10
next
value
20
next
value
30
next
value
40
next
value
50
next
None
tail
Click an operation to see how linked lists work

A node in Python

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None   # pointer to next node


class LinkedList:
    def __init__(self):
        self.head = None   # pointer to first node
        self.size = 0

Key operations and their complexity

OperationArrayLinked ListWhy
Access by indexO(1)O(n)Must walk from head
Insert at beginningO(n)O(1)Just update head pointer
Insert at endO(1) amortizedO(n) or O(1) with tailWalk to end or use tail pointer
Insert at positionO(n)O(n)Walk to position
Delete at beginningO(n)O(1)Just update head pointer
Delete at positionO(n)O(n)Walk to position
SearchO(n)O(n)Must traverse

Arrays vs linked lists — when to use which

SituationUse
Need fast index accessArray
Frequent insert/delete at beginningLinked list
Frequent insert/delete in middleLinked list
Fixed or known sizeArray
Unknown or dynamic sizeEither
Cache efficiency mattersArray
Memory is fragmentedLinked list

Arrays win on access speed — O(1) vs O(n). Linked lists win on insert/delete at the beginning — O(1) vs O(n). The right choice depends on what operations your program does most.


Types of linked lists

Singly linked — each node points to the next. Can only traverse forward.

Doubly linked — each node points to both next and previous. Can traverse in both directions.

Circular — the last node points back to the first. Used in round-robin scheduling.


What is coming in this section

Singly linked list — build one from scratch, implement all operations with visualization.

Doubly linked list — add backward pointers, see how insert and delete change.

Fast and slow pointers — the most important linked list technique. Detects cycles, finds midpoints, and more.

On this page