Menu
Coddy logo textTech

Linked List

Last updated

A linked list stores a sequence as a chain of nodes, where each node holds a value and a pointer to the next node. Unlike an array, the nodes aren't contiguous in memory - you follow the next pointers to walk the list. Press play above to watch nodes get linked at the head and tail, a value get searched for by walking the chain, and a node get removed by rewiring a pointer.

Inserting or deleting at the head is O(1) because you just repoint the head. Reaching a position in the middle or the tail is O(n) because you must walk there first. That trade-off - cheap ends, no random access - is what distinguishes a linked list from an array.

Time complexity

OperationComplexityNotes
Insert at headO(1)Repoint head
Insert at tailO(n)Walk to the end first (O(1) with a tail pointer)
SearchO(n)Follow next pointers
Delete headO(1)Repoint head past it
Access by indexO(n)No random access

Linked list vs array

AspectLinked listArray
MemoryScattered nodes + pointersContiguous block
Random accessO(n)O(1)
Insert/delete at frontO(1)O(n) (shift)
Cache friendlinessPoorGood

Worked example

Building the list [10, 20], prepending 5, then deleting 20:

StepStructureAction
Starthead -> nullEmpty list
Insert head 10head -> 10 -> nullRepoint head to a new node whose next is the old head (null)
Insert tail 20head -> 10 -> 20 -> nullWalk to node 10, set its next to a new node 20
Insert head 5head -> 5 -> 10 -> 20 -> nullNew node 5 points to old head 10; head now points to 5
Delete 20head -> 5 -> 10 -> nullWalk to 10, set its next from 20 to null; node 20 is unlinked

When to use a linked list

Use it whenAvoid it when
You insert or delete at the ends (or at a held node) far more than you indexYou need fast random access by position (O(1) indexing)
The size changes a lot and you want no resize/copy costYou iterate tight loops where cache locality dominates performance
You're building a queue, stack, or adjacency listMemory is tight - each node pays an extra pointer per element
You need stable references to nodes that survive insertions elsewhereYou mostly read and rarely mutate, so an array is simpler and faster

Linked List code

A clean, runnable Linked List implementation in Python, JavaScript, Java, C++, C. Pick a language, copy the code, or open it pre-loaded in the Coddy Playground.

Linked List code in Python

Python
1class Node:2    def __init__(self, value):3        self.value = value4        self.next = None5
6
7class LinkedList:8    def __init__(self):9        self.head = None10
11    def append(self, value):12        node = Node(value)13        if self.head is None:14            self.head = node15            return16        current = self.head17        while current.next:18            current = current.next19        current.next = node20
21    def find(self, value):22        current = self.head23        while current:24            if current.value == value:25                return True26            current = current.next27        return False28
29    def delete(self, value):30        # Re-link the previous node around the match31        current, prev = self.head, None32        while current:33            if current.value == value:34                if prev is None:35                    self.head = current.next36                else:37                    prev.next = current.next38                return True39            prev, current = current, current.next40        return False41
42    def __str__(self):43        values, current = [], self.head44        while current:45            values.append(str(current.value))46            current = current.next47        return " -> ".join(values) + " -> None"48
49
50lst = LinkedList()51for value in [3, 7, 1, 9]:52    lst.append(value)53
54print("List:    ", lst)55print("find(7): ", lst.find(7))56lst.delete(1)57print("After delete(1):", lst)
Run this code in the Python Playground

Linked List FAQ

What is the difference between a linked list and an array?
An array stores elements in one contiguous block, giving O(1) random access but O(n) inserts/deletes that shift elements. A linked list stores nodes anywhere in memory connected by pointers, giving O(1) inserts/deletes at a known position but O(n) access since you must walk the chain.
When should I use a linked list?
Linked lists shine when you frequently insert or remove at the ends (or at a node you already hold a reference to) and rarely need random access - for example, queues, stacks, adjacency lists, and LRU caches. If you need fast indexed access, an array or dynamic array is usually better.
What is the time complexity of a linked list?
Insert or delete at the head is O(1). Searching, or reaching the tail without a tail pointer, is O(n) because you follow next pointers one by one. There is no O(1) random access - indexing into a linked list is O(n).
What is the difference between a singly and doubly linked list?
A singly linked list stores only a next pointer per node, so you can walk in one direction and deleting a node requires a reference to its predecessor. A doubly linked list adds a prev pointer, enabling backward traversal and O(1) deletion of a held node, at the cost of an extra pointer per node and more bookkeeping on every insert and delete.
Why is inserting at the tail O(n) if inserting at the head is O(1)?
Inserting at the head only rewires the head pointer, which is constant work. Reaching the tail means following next pointers from the head all the way to the end, which is O(n). Keeping a separate tail pointer makes tail insertion O(1) too, which is why real-world lists often track both ends.
Do linked lists have O(1) insertion everywhere?
No - that's a common misconception. The insertion itself is O(1) only once you already hold a pointer to the node you're inserting after. Finding that position by value or index still costs O(n) because you must walk the chain to get there.
Coddy programming languages illustration

Master algorithms with Coddy

GET STARTED