Python linked list O1 insert/remove
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A linked list supports O(1) insertion and removal at the head (and tail, if doubly linked) because these operations only update a constant number of pointers. Python does not have a built-in linked list class, but collections.deque provides O(1) append and pop at both ends. For a custom implementation, a doubly linked list with sentinel nodes gives the cleanest O(1) operations and simplifies edge case handling.
Singly Linked List
Insertion and removal at the head are O(1) because they only modify the head pointer.
Doubly Linked List
A doubly linked list allows O(1) operations at both ends:
O(1) Removal by Node Reference
The key advantage of a doubly linked list is O(1) removal when you have a direct reference to the node:
This pattern is used in LRU caches where a hash map stores node references for O(1) lookup and the linked list maintains order.
LRU Cache Example
An LRU cache combines a dictionary (O(1) lookup) with a doubly linked list (O(1) reorder and eviction):
Using collections.deque
Python's deque provides O(1) append and pop at both ends:
deque is implemented as a doubly linked list of fixed-size blocks, giving O(1) at both ends.
Operation Complexity Comparison
| Operation | Singly Linked | Doubly Linked | deque | list |
| Insert at head | O(1) | O(1) | O(1) | O(n) |
| Insert at tail | O(n) | O(1) | O(1) | O(1) amortized |
| Remove from head | O(1) | O(1) | O(1) | O(n) |
| Remove from tail | O(n) | O(1) | O(1) | O(1) |
| Remove by reference | O(n) | O(1) | N/A | N/A |
| Access by index | O(n) | O(n) | O(n) | O(1) |
Common Pitfalls
- Forgetting to update both
prevandnextpointers: In a doubly linked list, every insertion or removal must update pointers in both directions. Missing one pointer creates a broken list that appears to work in one traversal direction but fails in the other. - Not using sentinel nodes: Without dummy head and tail nodes, every insertion and removal must check for
None(empty list, single-element list). Sentinel nodes eliminate these edge cases and simplify the code. - O(n) removal without a node reference: Removing a value from a linked list by searching for it is O(n), not O(1). O(1) removal requires a direct reference to the node, typically stored in a hash map.
- Using Python
listas a linked list:list.insert(0, x)is O(n) because it shifts all elements. Usedeque.appendleft()for O(1) head insertion or implement a proper linked list. - Memory overhead: Each linked list node stores two pointer references (16+ bytes each) in addition to the data. For small data like integers, the overhead can be several times larger than the data itself. Use
dequeor arrays when memory efficiency matters.
Summary
- Singly linked lists support O(1) insert/remove at the head only
- Doubly linked lists support O(1) insert/remove at both head and tail
- O(1) removal by node reference requires a doubly linked list and a stored reference to the node
- Use sentinel (dummy) head and tail nodes to eliminate edge cases
collections.dequeprovides O(1) operations at both ends and is the preferred choice for most use cases- The LRU cache pattern combines a hash map with a doubly linked list for O(1) lookup, insertion, and eviction
Related reading
- Python list sort in descending order
- Python maximum recursion depth exceeded while calling a Python object
- Python NEAT not learning further after a certain point
- Python non-greedy regexes
- python list by value not by reference
- Python List of dict, if exists increment a dict value, if not append a new dict
- Python list directory, subdirectory, and files
- Python list vs. array – when to use?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.