Operations on Linked Lists
Linked lists support several essential operations that allow you to manipulate the
data in the list. These operations vary depending on the type of linked list (singly,
doubly, or circular). Below are some of the most common operations on linked
lists.
1. Basic Operations
1.1 Traversal
Traversal refers to the process of visiting each node in the linked list. This
operation is typically used to display all elements of the list or search for a
particular element.
Singly Linked List: Start from the head and follow the next pointers until
reaching null.
Doubly Linked List: Traverse from the head to the last node and back using
both next and prev pointers.
Circular Linked List: Traverse starting from the head and continue until
reaching the head again.
Example (Python - Singly Linked List):
def traverse(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
1.2 Insertion
Insertion involves adding a new node at a specific position in the linked list.
Common insertion operations include:
, At the beginning: Insert the node at the head of the list.
At the end: Insert the node at the last position.
At a specific position: Insert the node at a given index.
Example (Python - Insert at the Beginning for Singly Linked List):
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
Insert at the End for Singly Linked List:
def insert_at_end(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
current = self.head
while current.next:
current = current.next
current.next = new_node
1.3 Deletion
Deletion involves removing a node from the linked list. The most common
deletions are:
Delete from the beginning: Remove the head node.
Delete from the end: Remove the last node.
Delete by value: Remove a specific node containing a given value.
Delete by position: Remove the node at a specific index.
Example (Python - Delete from the Beginning for Singly Linked List):
def delete_from_beginning(self):
if self.head:
self.head = self.head.next
Linked lists support several essential operations that allow you to manipulate the
data in the list. These operations vary depending on the type of linked list (singly,
doubly, or circular). Below are some of the most common operations on linked
lists.
1. Basic Operations
1.1 Traversal
Traversal refers to the process of visiting each node in the linked list. This
operation is typically used to display all elements of the list or search for a
particular element.
Singly Linked List: Start from the head and follow the next pointers until
reaching null.
Doubly Linked List: Traverse from the head to the last node and back using
both next and prev pointers.
Circular Linked List: Traverse starting from the head and continue until
reaching the head again.
Example (Python - Singly Linked List):
def traverse(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")
1.2 Insertion
Insertion involves adding a new node at a specific position in the linked list.
Common insertion operations include:
, At the beginning: Insert the node at the head of the list.
At the end: Insert the node at the last position.
At a specific position: Insert the node at a given index.
Example (Python - Insert at the Beginning for Singly Linked List):
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
Insert at the End for Singly Linked List:
def insert_at_end(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
return
current = self.head
while current.next:
current = current.next
current.next = new_node
1.3 Deletion
Deletion involves removing a node from the linked list. The most common
deletions are:
Delete from the beginning: Remove the head node.
Delete from the end: Remove the last node.
Delete by value: Remove a specific node containing a given value.
Delete by position: Remove the node at a specific index.
Example (Python - Delete from the Beginning for Singly Linked List):
def delete_from_beginning(self):
if self.head:
self.head = self.head.next