Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to delete a new node from the beginning of the doubly linked list
When you need to delete a node from the beginning of a doubly linked list, you must create a Node class with three attributes: the data, a pointer to the next node, and a pointer to the previous node. This operation involves updating the head pointer and adjusting the previous pointer of the new head node.
Node Class Structure
First, let's define the Node class and the doubly linked list structure ?
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
Adding Nodes to the List
The add_data method adds new nodes to the end of the list ?
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
def add_data(self, my_data):
new_node = Node(my_data)
if self.head == 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
def print_it(self):
curr = self.head
if self.head == None:
print("The list is empty")
return
print("The nodes in the doubly linked list are :")
while curr != None:
print(curr.data)
curr = curr.next
Deleting from the Beginning
The delete_from_beginning method removes the first node and updates the head pointer ?
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
def add_data(self, my_data):
new_node = Node(my_data)
if self.head == 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
def print_it(self):
curr = self.head
if self.head == None:
print("The list is empty")
return
print("The nodes in the doubly linked list are :")
while curr != None:
print(curr.data)
curr = curr.next
def delete_from_beginning(self):
if self.head == None:
return
else:
if self.head != self.tail:
self.head = self.head.next
self.head.prev = None
else:
self.head = self.tail = None
# Create instance and test deletion
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_it()
# Delete one node from beginning
my_list.delete_from_beginning()
print("\nAfter deleting one element from the beginning:")
my_list.print_it()
Elements are being added to the doubly linked list The nodes in the doubly linked list are : 10 24 54 77 92 After deleting one element from the beginning: The nodes in the doubly linked list are : 24 54 77 92
How the Deletion Works
The deletion process follows these steps:
Key Points
- Always check if the list is empty before deletion
- Handle the case where there's only one node (head == tail)
- Update both the
headpointer and theprevpointer of the new head - The deletion operation has O(1) time complexity
Conclusion
Deleting from the beginning of a doubly linked list involves updating the head pointer to the next node and setting the new head's previous pointer to None. This operation is efficient with constant time complexity and requires careful handling of edge cases.
