Skip to content

Linked List Implementation in Python: Effortlessly Create and Manipulate

[

Linked List Implementation in Python

In this tutorial, we will explore the implementation of linked lists in Python. Linked lists are a type of data structure that can be used to store and manipulate a collection of objects. Unlike traditional lists, linked lists do not store elements in a contiguous memory block. Instead, they store references to their data as part of their own elements.

Understanding Linked Lists

Before diving into the implementation details, let’s understand the basic concepts of linked lists.

Main Concepts

A linked list is a collection of nodes, where each node has two fields:

  1. Data: This field contains the value to be stored in the node.
  2. Next: This field contains a reference to the next node in the list.

Here’s an example of what a typical node looks like:

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

In a linked list, the first node is called the head, and it serves as the starting point for any iteration through the list. The last node in the list has its next reference set to None to indicate the end of the list.

Practical Applications

Linked lists have various practical applications in the real world. They can be used to implement:

  • Stacks
  • Queues
  • Graphs
  • Hash tables

Implementing Your Own Linked List

Now that we understand the basics of linked lists, let’s implement our own linked list in Python.

Creating a Linked List

To create a linked list, we need to define a class that represents the linked list itself. This class will have methods to perform various operations on the list, such as inserting and deleting nodes.

class LinkedList:
def __init__(self):
self.head = None
def insert(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node

In the above code, we define a LinkedList class that has a head attribute initialized to None. The insert() method allows us to insert a new node at the end of the list. If the list is empty, the new node becomes the head. Otherwise, we traverse the list until we reach the last node and then attach the new node to the next field of the last node.

Traversing a Linked List

To traverse a linked list, we can simply iterate through each node starting from the head until we reach the end of the list.

def traverse(self):
current = self.head
while current:
print(current.data)
current = current.next

The traverse() method starts from the head and prints the data of each node in the list until it reaches the end.

Inserting/Deleting Nodes

To insert a new node at a specific position or delete a node from the list, we need to traverse the list and manipulate the references between nodes.

def insert_at_position(self, data, position):
new_node = Node(data)
current = self.head
prev = None
count = 0
while current and count < position:
prev = current
current = current.next
count += 1
if prev is None:
new_node.next = self.head
self.head = new_node
else:
new_node.next = current
prev.next = new_node
def delete(self, data):
current = self.head
prev = None
while current:
if current.data == data:
if prev is None:
self.head = current.next
else:
prev.next = current.next
return
prev = current
current = current.next

The insert_at_position() method inserts a new node with the given data at the specified position in the list. The delete() method removes the node with the given data from the list. In both cases, we iteratively traverse the list to find the desired position or node to be deleted.

Conclusion

In this tutorial, we have explored the implementation of linked lists in Python. We have learned the main concepts of linked lists, such as nodes and their references, and how to create, traverse, insert, and delete nodes in a linked list. Linked lists can be a useful data structure for certain scenarios, and understanding their implementation can enhance your problem-solving skills in Python.