Back to Data Structures & Algorithms
2025-12-275 min read

Linked List Operations (Traverse, Insert, Delete) (Data Structures & Algorithms)

Learn Linked List Operations (Traverse, Insert, Delete) (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

Linked lists are a fundamental data structure used to store collections of data elements. Unlike arrays, linked lists allow for dynamic memory allocation, making them ideal for handling data with varying sizes. In this lesson, we'll delve into the essential operations of traversing, inserting, and deleting nodes in a Python linked list.

Why This Matters

Understanding linked list operations is crucial for solving real-world problems, such as implementing efficient algorithms and debugging complex programs. These skills are highly sought after by interviewers and can help you tackle challenging coding tasks. Additionally, understanding linked lists can provide valuable insights into memory management and data structures.

Prerequisites

Before diving into the core concept, ensure you have a solid grasp of the following:

  • Python syntax and control structures (if statements, for loops)
  • Basic data types (integers, strings, lists)
  • Understanding of classes and objects in Python

Core Concept

Linked List Basics

A linked list is composed of nodes, where each node contains a data field and a reference to the next node. The first node in a linked list is called the head, while the last node (if any) is referred to as the tail. A linked list can be either singly or doubly linked, depending on whether each node has a reference to both its preceding and succeeding nodes. In this lesson, we'll focus on singly linked lists.

Creating a Linked List Node

To create a linked list in Python, we first need to define a Node class with two attributes: data and next.

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

Traversing a Linked List

Traversing a linked list involves iterating through each node until we reach the end (tail) of the list. To traverse a linked list, we start at the head and follow the next references from one node to the next. Here's an example of traversing a simple linked list:

head = Node(1)
second = Node(2)
third = Node(3)

head.next = second
second.next = third

To traverse this linked list, we can use a helper function like the following:

def print_list(node):
while node is not None:
print(node.data)
node = node.next

Inserting a Node

To insert a new node into a linked list, we need to perform the following steps:

  1. Create a new Node with the desired data.
  2. If the linked list is empty (head is None), set the new node as the head.
  3. Otherwise, traverse the linked list until you find the position where the new node should be inserted.
  4. Update the next reference of the preceding node to point to the new node.
  5. Set the next reference of the new node to the original successor (if any).

Here's an example of inserting a new node at the beginning of a linked list:

new_node = Node(0)
new_node.next = head
head = new_node

Deleting a Node

Deleting a node from a linked list involves finding the target node and updating its predecessor's next reference to skip over the deleted node. If the node to be deleted is the head, we simply update the head to point to the next node in the list (if any). Here's an example of deleting a node from a linked list:

def delete_node(node):
if node is None or node.next is None:
return

node.data = node.next.data
node.next = node.next.next

Worked Example

Let's create a linked list and perform various operations on it:

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

head = Node(1)
second = Node(2)
third = Node(3)
fourth = Node(4)
fifth = Node(5)

head.next = second
second.next = third
third.next = fourth
fourth.next = fifth

print("Original linked list:")
def print_list(node):
while node is not None:
print(node.data, end=" -> ")
node = node.next
print("None")

print_list(head)

Insert a new node at the beginning of the list

new_node = Node(0)

new_node.next = head

head = new_node

print("\nLinked list after inserting 0:")

print_list(head)

Delete the second node (2) from the list

def delete_node(node):

if node is None or node.next is None:

return

node.data = node.next.data

node.next = node.next.next

delete_node(second)

print("\nLinked list after deleting 2:")

print_list(head)

Common Mistakes

  1. Forgetting to update the next reference when inserting a new node.
  2. Trying to delete the head without updating the second node's next reference (if any).
  3. Not handling cases where the linked list is empty or contains only one node.
  4. Using an infinite loop when traversing the linked list due to improper termination conditions.
  5. Forgetting to define the data attribute for each new node during insertion.

Practice Questions

  1. Write a function to append a new node with a given data value at the end of a linked list.
  2. Implement a function that reverses a singly linked list.
  3. Write a function to find the middle node in a singly linked list (if the list has an odd number of nodes, consider the middle node before the last one).
  4. Given two singly linked lists, write a function that merges them in sorted order to form a new singly linked list.
  5. Write a function to delete all occurrences of a given value from a singly linked list.

FAQ

How do I check if a linked list is empty?

Check if the head node is None.

What is the time complexity of inserting a new node at the beginning of a singly linked list?

The time complexity for inserting a new node at the beginning is O(1), as it only requires updating the head reference and creating a new node.

What is the time complexity of deleting a node from a singly linked list?

The time complexity for deleting a node is O(n), where n is the number of nodes in the list, because we need to traverse the list to find the target node and update its predecessor's next reference.

Can I use a doubly linked list to efficiently delete a node from the middle?

Yes, using a doubly linked list allows for O(1) time complexity when deleting a node from the middle, as each node has a reference to its predecessor and successor.

What are some advantages of using a singly linked list over an array?

Singly linked lists allow for dynamic memory allocation, making them suitable for handling data with varying sizes. Additionally, singly linked lists can be more efficient in terms of memory usage when dealing with large amounts of data that do not fit into contiguous memory blocks.

Linked List Operations (Traverse, Insert, Delete) (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn