Python program to delete a new node from the middle of the doubly linked list

When it is required to delete a node from the middle of a doubly linked list, a Node class needs to be created with three attributes: the data, access to the next node, and access to the previous node.

In a doubly linked list, each node has pointers to both the next and previous nodes, allowing bidirectional traversal. The middle node deletion requires finding the center position and updating the surrounding node pointers.

Node Class Structure

The Node class stores data and maintains references to adjacent nodes ?

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

Doubly Linked List Implementation

Here's the complete implementation with middle node deletion functionality ?

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

class DoublyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None
        self.size = 0

    def add_data(self, my_data):
        new_node = Node(my_data)
        if self.head is None:
            self.head = self.tail = new_node
            self.head.prev = None
            self.tail.next = None
        else:
            self.tail.next = new_node
            new_node.prev = self.tail
            self.tail = new_node
            self.tail.next = None
        self.size += 1

    def print_list(self):
        curr = self.head
        if self.head is None:
            print("The list is empty")
            return
        print("The nodes in the doubly linked list are:")
        while curr is not None:
            print(curr.data)
            curr = curr.next

    def delete_from_middle(self):
        if self.head is None:
            return
        
        # Find middle position
        mid = (self.size // 2) if (self.size % 2 == 0) else ((self.size + 1) // 2)
        curr = self.head
        
        # Navigate to middle node
        for i in range(1, mid):
            curr = curr.next
        
        # Update pointers based on position
        if curr == self.head:
            self.head = curr.next
            if self.head:
                self.head.prev = None
        elif curr == self.tail:
            self.tail = self.tail.prev
            if self.tail:
                self.tail.next = None
        else:
            curr.prev.next = curr.next
            curr.next.prev = curr.prev
        
        self.size -= 1

# Example usage
my_list = DoublyLinkedList()
print("Elements are being added to the doubly linked list")
my_list.add_data(10)
my_list.add_data(24)
my_list.add_data(54)
my_list.add_data(77)
my_list.add_data(92)
my_list.print_list()

print("\nDeleting middle elements one by one:")
while my_list.head is not None:
    my_list.delete_from_middle()
    print("After deleting middle element:")
    my_list.print_list()
Elements are being added to the doubly linked list
The nodes in the doubly linked list are:
10
24
54
77
92

Deleting middle elements one by one:
After deleting middle element:
The nodes in the doubly linked list are:
10
24
77
92
After deleting middle element:
The nodes in the doubly linked list are:
10
77
92
After deleting middle element:
The nodes in the doubly linked list are:
10
92
After deleting middle element:
The nodes in the doubly linked list are:
92
After deleting middle element:
The list is empty

How the Middle Deletion Works

The algorithm follows these steps:

  1. Calculate middle position: Use size // 2 for even length lists
  2. Navigate to middle: Traverse from head to the calculated position
  3. Update pointers: Reconnect previous and next nodes
  4. Handle edge cases: Special handling for head and tail nodes

Key Points

Component Purpose Key Attribute
Node class Store data and pointers prev, data, next
DoublyLinkedList Manage list operations head, tail, size
Middle deletion Remove center element Pointer reconnection

Conclusion

Deleting from the middle of a doubly linked list requires careful pointer management to maintain list integrity. The algorithm finds the middle position and updates adjacent node connections appropriately.

Updated on: 2026-03-25T17:18:01+05:30

391 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements