Contents

Computer Science › Data Structures

Linked List

Nodes pointing to the next node: fast inserts, slow random access.

Also known as: singly linked list, doubly linked list

A linked list is a chain of nodes, where each node holds a value and a reference to the next node. Unlike an array, the nodes can sit anywhere in memory.

head ─► [3 | ●]─► [7 | ●]─► [9 | ●]─► None
class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

head = Node(3, Node(7, Node(9)))

node = head
while node:                      # walk the list
    print(node.value)
    node = node.next

A doubly linked list also keeps a reference to the previous node, so you can walk both ways (doubly linked list).

Costs

OperationLinked listArray
Access the nth itemlinear: walk from the headconstant
Insert or remove at the frontconstantlinear
Insert or remove after a node you holdconstantlinear
Search by valuelinearlinear
Memoryextra pointer per itemcompact

When they’re useful

  • Building queues and stacks with cheap ends.
  • Frequent insertions and removals in the middle, when you already hold the node (LRU caches combine them with a hash map, LRU cache).
  • Teaching pointers and recursion, and as the basis for other structures.

When they’re not

In practice, dynamic arrays usually win even for jobs that look like linked-list jobs. Arrays are contiguous in memory, which the CPU cache loves, while a linked list chases pointers all over memory. You rarely write a linked list yourself, but the idea is everywhere, and a classic interview topic.

Watch out for: forgetting the empty list, losing the rest of the list when you reassign next, and cycles that cause infinite loops.