Linked List
Python Programming
Data Structures
Python Coding
Linked List Implementation

How can I use a Linked List in Python?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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:

python
1from dataclasses import dataclass
2from typing import Any, Optional
3
4@dataclass
5class Node:
6    value: Any
7    next: Optional["Node"] = None
8
9class LinkedList:
10    def __init__(self):
11        self.head: Optional[Node] = None
12
13    def append(self, value):
14        new_node = Node(value)
15
16        if self.head is None:
17            self.head = new_node
18            return
19
20        current = self.head
21        while current.next is not None:
22            current = current.next
23        current.next = new_node
24
25    def prepend(self, value):
26        self.head = Node(value, self.head)
27
28    def to_list(self):
29        values = []
30        current = self.head
31        while current is not None:
32            values.append(current.value)
33            current = current.next
34        return values

Usage:

python
1linked = LinkedList()
2linked.append(10)
3linked.append(20)
4linked.prepend(5)
5
6print(linked.to_list())

This prints:

python
[5, 10, 20]

Because a singly linked list stores only the next reference, you traverse it from the head node:

python
1def contains(self, target):
2    current = self.head
3    while current is not None:
4        if current.value == target:
5            return True
6        current = current.next
7    return False

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:

python
1def remove(self, target):
2    current = self.head
3    previous = None
4
5    while current is not None:
6        if current.value == target:
7            if previous is None:
8                self.head = current.next
9            else:
10                previous.next = current.next
11            return True
12        previous = current
13        current = current.next
14
15    return False

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:

  • 'list for general-purpose ordered storage'
  • 'collections.deque for fast appends and pops on both ends'

Example with deque:

python
1from collections import deque
2
3queue = deque([1, 2, 3])
4queue.appendleft(0)
5queue.append(4)
6
7print(queue)

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 list would 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 Node objects and a head pointer.
  • Linked lists are useful for learning and for explicit node-manipulation problems.
  • Traversal and search are linear because nodes are visited one by one.
  • 'list and collections.deque are often better choices in real Python programs.'
  • Use a custom linked list only when its node-based structure is genuinely needed.

Course illustration
Course illustration

All Rights Reserved.