Building a Linked List in Python with Examples
A linked list is a linear data structure where elements are not stored next to each other in memory. Unlike and array, elements in a linked list use pointers or references to each other to keep the list intact.
Like arrays or traditional lists, linked lists are an ordered collection of objects. Linked lists stand apart from lists in how they store elements in memory. While regular lists like arrays and slices use a contiguous memory block to store references to their data linked lists store references or pointers as part of each element.

A normal list is just a pointer to the first element in the list, and a specific item can be retrieved by providing a memory offset.
A linked list is also just a pointer to the first element in the list, but memory offsets won’t do us any good. We need to examine the first element’s next pointer to see where the next item is, then we can navigate to it. From there, we can find the next item and so on down the list.
Python singly linked list example
Node Class
First, we’ll build a Node class. The LinkedList class we eventually build will be a list of Nodes.
class Node:
def __init__(self, val):
self.val = val
self.next = None
def set_next(self, node):
self.next = node
def __repr__(self):
return self.val
Each node has a val data member (the information it stores) and a next data member. The next data member just points to the next Node in the list if there is one, otherwise, it’s None
Linked List Constructor
class LinkedList:
def __init__(self):
self.head = None
The constructor is easy - just initialize an empty head pointer. This indicates we now have an empty list.
Iterating over the list
Let’s make it easy to iterate over each item in the list using Python’s for _ in _ syntax.
def __iter__(self):
node = self.head
while node is not None:
yield node
node = node.next
By implementing Python’s __iter__ method, we can now use iteration syntax. For example, for item in linked_list:.
Adding to the linked list
Let’s create a way to add items to the tail of the list, the add_to_tail method. It takes a node as input, iterates over the entire list, then adds the given node to the end.
def add_to_tail(self, node):
if self.head == None:
self.head = node
return
for current_node in self:
pass
current_node.set_next(node)
Removing from the linked list
There are other ways to remove items from the list, but for now, and as an example, let’s write a remove from head method.
def remove_from_head(self):
if self.head == None:
return None
temp = self.head
self.head = self.head.next
return temp
remove_from_head removes and returns the first item from the list, assuming one exists.
Printing the linked list
Last but not least, we can implement Python’s __repr__() method so that we can call print() directly on a list and control what it printed. Here’s a representation I like:
def __repr__(self):
nodes = []
for node in self:
nodes.append(node.val)
return " -> ".join(nodes)
This method will print each node’s value in order, with arrows in between. For example, hello -> this -> is -> my -> list.
Using the linked list
linked_list = LinkedList()
linked_list.add_to_tail(Node('john'))
linked_list.add_to_tail(Node('sally'))
linked_list.add_to_tail(Node('jimmy'))
print("ll:", linked_list)
first = linked_list.remove_from_head()
print("removed:", first)
print("ll:", linked_list)
Practical Applications of a Linked List
Linked lists are immensely valuable in computer science because they uniquely allow us to add and remove elements anywhere in the list quickly, with a Big-O complexity of just O(1).
Big-O complexity of a linked list
| Operation | Big-O Complexity |
|---|---|
| Insert | O(1) |
| Delete | O(1) |
| Index | O(n) |
Because of the fast operations, linked lists are used practically in many different scenarios, including:
- Stacks
- Queues
- Hash maps, to prevent collisions
- Undo/Redo operations (stack)
- Appending a song to a playlist
- To keep items in the same place in memory for performance reasons
Full Linked List Code Sample
class LinkedList:
def __init__(self):
self.head = None
def __iter__(self):
node = self.head
while node is not None:
yield node
node = node.next
def __repr__(self):
nodes = []
for node in self:
nodes.append(node.val)
return " -> ".join(nodes)
def add_to_tail(self, node):
if self.head == None:
self.head = node
return
for current_node in self:
pass
current_node.set_next(node)
def remove_from_head(self):
if self.head == None:
return None
temp = self.head
self.head = self.head.next
return temp
class Node:
def __init__(self, val):
self.val = val
self.next = None
def set_next(self, node):
self.next = node
def __repr__(self):
return self.val
Related Articles
Should you Learn Computer Science or Software Engineering?
Dec 17, 2020 by Winston Wagner - Technical author at Boot.dev
The most important thing to understand about these two fields of study is that, ultimately, they are similar. At the end of the day, Software Engineering and Computer Science will both help to make you a better programmer and developer, and the only difference between the two is how they are applied. Software Engineering tends to be more practical, and Computer Science tends to be more theoretical. In a way, Software Engineering is just applied Computer Science, and using that as a starting point, we can examine the differences between the two.
Guide to Getting a Certificate in Computer Science
Dec 15, 2020 by Zulie Rane - Data analysis and computer science techincal author
There are so many reasons to want to get a certificate in computer science in 2021, especially when you compare it to alternatives like getting a degree or attending a coding bootcamp.
6 Computer Science Resume Examples
Dec 14, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
It’s really hard to get your foot in the door for engineering interviews, especially if you have no experience and are looking for an entry-level position. Often times, more experienced candidates looking to find a higher-paying job can also have trouble. As an employer myself, I can tell you that one of the biggest mistakes I see in 75% of resumes is using a visually boring template. When I’m sifting through forty or fifty applicants, it’s really easy for my eyes to glaze over. Think of your resume as your website landing page. You need to catch your employer’s attention by calling out your biggest accomplishments and selling points at a glance.
The Highest-Paying Computer Science Jobs
Dec 09, 2020 by Lane Wagner - Boot.dev co-founder and backend engineer
There are many jobs within the software industry, and most of them are easier to land or are higher-paying once you land them, if you have a solid grasp of computer science fundamentals. You don’t need a degree from an accredited university in 90% of cases, but you do need to learn the material, whether it be online, on the job, or in a formal setting. Let’s explore the most common computer science job titles and their associated compensation, details, and duties.