Introduction
Data structures play a crucial role in computer science, enabling efficient organization and manipulation of data. One such fundamental data structure is the singly linked list. In this blog post, we will explore what singly linked lists are, their advantages, and how to implement them in Python.
What is a Singly Linked List?
A singly linked list is a linear data structure in which elements are stored in nodes. Each node contains data and a reference (or link) to the next node in the sequence. The last node points to None
, indicating the end of the list. This structure allows for dynamic memory allocation and efficient insertion and deletion operations.
Basic Components of a Singly Linked List
Node
The building block of a singly linked list is the node. It typically contains two fields:
Data: The actual information stored in the node.
Next: A reference to the next node in the sequence.
Let’s represent a node in Python:
class Node:
def __init__(self, data=None):
self.data = data
self.next_node = None
SinglyLinkedList
The SinglyLinkedList
class serves as the container for the nodes. It usually contains a reference to the first node (head) and provides methods for common operations.
class SinglyLinkedList:
def __init__(self):
self.head = None
Operations on Singly Linked Lists
1. Insertion
Inserting a new node can be done at the beginning, end, or at a specific position in the list.
Insert at the Beginning
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next_node = self.head
self.head = new_node
Insert at the End
def insert_at_end(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
current_node = self.head
while current_node.next_node:
current_node = current_node.next_node
current_node.next_node = new_node
2. Deletion
Deletion can be performed by removing a node with a specific value or at a given position.
Delete by Value
def delete_by_value(self, value):
current_node = self.head
if current_node and current_node.data == value:
self.head = current_node.next_node
return
prev_node = None
while current_node and current_node.data != value:
prev_node = current_node
current_node = current_node.next_node
if current_node is None:
return
prev_node.next_node = current_node.next_node
Delete at Position
def delete_at_position(self, position):
current_node = self.head
if position == 0:
self.head = current_node.next_node
return
prev_node = None
count = 0
while current_node and count != position:
prev_node = current_node
current_node = current_node.next_node
count += 1
if current_node is None:
return
prev_node.next_node = current_node.next_node
3. Traversal
def traverse(self):
current_node = self.head
while current_node:
print(current_node.data, end=" -> ")
current_node = current_node.next_node
print("None")
Putting It All Together
Now, let’s demonstrate the usage of the singly linked list with a simple example:
# Creating a singly linked list
linked_list = SinglyLinkedList()
# Inserting elements
linked_list.insert_at_end(1)
linked_list.insert_at_end(2)
linked_list.insert_at_end(3)
# Traversing the list
print("Original Linked List:")
linked_list.traverse()
# Deleting a node
linked_list.delete_by_value(2)
# Traversing again after deletion
print("\nLinked List after Deletion:")
linked_list.traverse()
Conclusion
This blog post discusses the concept of singly linked lists, a fundamental data structure in computer science. It explains the structure of a singly linked list, its basic components like Node and SinglyLinkedList, and the operations that can be performed on it such as insertion, deletion, and traversal. The post also includes Python codes for these operations and a demonstration of how to create and manipulate a singly linked list.