Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Create a Linked List in Python

Learn the fundamental concept of linked lists and create your own implementation in Python. This tutorial provides a step-by-step guide, code snippets, and explanations for beginners and experts alike …


Updated July 23, 2023

Learn the fundamental concept of linked lists and create your own implementation in Python. This tutorial provides a step-by-step guide, code snippets, and explanations for beginners and experts alike.

What is a Linked List?

A linked list is a linear data structure where each element (called a node) points to the next element in the sequence. Unlike arrays, linked lists do not have a fixed size and can grow or shrink dynamically as elements are added or removed. This makes them particularly useful when dealing with dynamic memory allocation.

Step 1: Defining the Node Class

In Python, we’ll define a Node class to represent each element in the linked list.

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

Here:

  • We create an initializer (__init__) method that takes a value parameter.
  • The value attribute stores the actual data for this node.
  • The next attribute points to the next node in the sequence. Initially, it’s set to None.

Step 2: Creating the LinkedList Class

Now, let’s create a LinkedList class that will manage our linked list.

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, value):
        new_node = Node(value)
        if not self.head:
            self.head = new_node
        else:
            current = self.head
            while current.next:
                current = current.next
            current.next = new_node

Here:

  • We create an initializer (__init__) method for the LinkedList class.
  • The head attribute points to the first node in the list. Initially, it’s set to None.
  • The append method adds a new node with the given value at the end of the list.

Step 3: Exploring Additional Methods

Let’s implement some additional methods for our linked list.

class LinkedList:
    # ... (previous code remains the same)

    def traverse(self):
        current = self.head
        while current:
            print(current.value, end=" ")
            current = current.next

    def delete(self, value):
        if not self.head:
            return

        if self.head.value == value:
            self.head = self.head.next
            return

        current = self.head
        while current.next:
            if current.next.value == value:
                current.next = current.next.next
                return
            current = current.next

Here:

  • The traverse method iterates through the list and prints each node’s value.
  • The delete method removes the first occurrence of a specified value in the list.

Example Use Case

Now that we have our linked list implementation, let’s see how it works.

my_list = LinkedList()
my_list.append(5)
my_list.append(10)
my_list.append(15)

print("Traversing the list:")
my_list.traverse()  # Output: 5 10 15

print("\nDeleting a node with value 10:")
my_list.delete(10)
my_list.traverse()  # Output: 5 15

In this example:

  • We create an instance of our LinkedList class.
  • We append three nodes to the list using the append method.
  • We traverse the list and print each node’s value using the traverse method.
  • Finally, we delete a node with value 10 from the list using the delete method.

By following this step-by-step guide, you’ve now learned how to create your own linked list implementation in Python!

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp