How can I use a Linked List in Python?
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
Python does not have a built-in linked-list type in the same way it has list, but you can absolutely implement one with classes. The more important question is whether you should, because in normal Python code list or collections.deque is often the better tool.
When A Linked List Makes Sense
A linked list stores each element in a node that points to the next node. This makes insertion or removal near a known node cheap, because you update links rather than shifting an entire array.
In Python, a linked list is most useful when:
- you are learning data structures
- you need explicit node manipulation
- you want to model chains, queues, or custom graph-like structures
If you only need append, pop, indexing, and iteration, Python's built-in containers are usually simpler and faster.
A Simple Singly Linked List
Here is a basic implementation:
Usage:
This prints:
Traversal And Search
Because a singly linked list stores only the next reference, you traverse it from the head node:
Unlike a normal Python list, there is no efficient random access. Reaching the tenth item means walking through the first nine.
Deleting A Node
Removal is one of the classic linked-list operations:
This is efficient once you reach the node, because you only rewire references. But the search to find the node is still linear.
Why Python list Or deque Is Often Better
Many Python developers reach for a linked list when they really want:
- '
listfor general-purpose ordered storage' - '
collections.dequefor fast appends and pops on both ends'
Example with deque:
If you do not need custom node references, deque is usually more practical than writing your own linked list.
A Good Mental Model
Think of a linked list as a chain of boxes where each box stores a value and the address of the next box. That is conceptually useful, especially when learning trees, graphs, and pointer-style algorithms.
But in idiomatic Python, custom linked lists are mostly educational or domain-specific rather than a default everyday structure.
Common Pitfalls
- Using a linked list when a normal Python
listwould be simpler and faster overall. - Expecting constant-time indexing. Linked lists do not support that efficiently.
- Forgetting to handle head-node removal as a special case.
- Writing recursive traversal for large lists and risking recursion-depth problems.
- Not keeping a tail pointer when frequent append performance matters.
Summary
- You can implement a linked list in Python with
Nodeobjects and aheadpointer. - Linked lists are useful for learning and for explicit node-manipulation problems.
- Traversal and search are linear because nodes are visited one by one.
- '
listandcollections.dequeare often better choices in real Python programs.' - Use a custom linked list only when its node-based structure is genuinely needed.
Related reading
- How can I use a Swift enum as a Dictionary key? Conforming to Equatable
- How can I use arrayExists function when the array contains a null value?
- How can I use if/else in a dictionary comprehension?
- How can I use list comprehensions to process a nested list?
- How can I use a Python script in the command line without cd-ing to its directory? Is it the PYTHONPATH?
- How can I use newline 'n' in an f-string to format a list of strings?
- How can I use pickle to save a dict or any other Python object?
- How can I use something like stdvectorstdmutex?

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.