Python has no built-in linked list, but you can implement one with a small Node class that stores a value and a reference to the next node, plus a list class that keeps track of the head. A singly linked list gives O(1) insertion and removal at the head and O(n) search, while a doubly linked list adds a prev link so you can walk and delete in both directions.
In day-to-day code, prefer a list or collections.deque. Write your own linked list when you are learning data structures, preparing for interviews, or need stable node references for operations such as an LRU cache.
A linked list is a chain of nodes. Each node holds data and a pointer to its neighbour. Unlike an array, nodes can be anywhere in memory, so inserting or removing a node only means re-pointing references, not shifting elements.
| Operation | Python list | Singly linked list | Doubly linked list |
|---|---|---|---|
| Index access | O(1) | O(n) | O(n) |
| Insert at head | O(n) | O(1) | O(1) |
| Insert at tail | O(1) amortised | O(n), or O(1) with a tail pointer | O(1) with a tail pointer |
| Delete a known node | O(n) | O(n) | O(1) |
| Search | O(n) | O(n) | O(n) |
Because each Python object carries overhead, a linked list uses far more memory than a list and is usually slower in practice. Use it for its algorithmic properties, not for speed.
This version keeps a tail pointer so append is O(1), supports iteration, and reverses in place.
Usage:
The remove method tracks the previous node so it can bypass the target, and it updates tail when the last node is removed. The tuple assignment inside reverse flips each next pointer while walking the list once.
A doubly linked list stores both next and prev, so a node can be unlinked in O(1) once you hold a reference to it.
Setting eq=False in the dataclass keeps nodes compared by identity and avoids infinite recursion from comparing circular prev and next links.
In a circular linked list the last node points back to the first, which is useful for round-robin scheduling. You can get the same behaviour from itertools.cycle or collections.deque.rotate.
For queues and stacks, use collections.deque: it is implemented as a doubly linked list of blocks in C, has O(1) appends and pops at both ends, and is much faster than any pure Python linked list.
No. The closest built-in is collections.deque, which is optimised for fast operations at both ends. Python's list is a dynamic array, not a linked list.
When you frequently insert or remove at known positions or at the front, and hold direct node references. For random access and most general use, a list is both simpler and faster.
Use Floyd's tortoise and hare algorithm: advance one pointer by one node and another by two. If they ever meet, the list contains a cycle.
You are reading .next or .data on a None reference, usually at the end of the list or on an empty list. Check for None before dereferencing, as the loops above do.
A linked list is a chain of nodes connected by references. Use a singly linked list with a tail pointer for O(1) appends and prepends, a doubly linked list when you need O(1) removal of a known node, and collections.deque when you just need a fast queue in production code.