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
Selected Reading
Python program to delete a new node from the end of the doubly linked list
A doubly linked list is a data structure where each node contains three components: data, a pointer to the next node, and a pointer to the previous node. To delete a node from the end, we need to update the tail pointer and handle edge cases properly.
Node Class Structure
First, we create a Node class to represent individual elements in the doubly linked list ?
class Node:
def __init__(self, my_data):
self.prev = None
self.data = my_data
self.next = None
Doubly Linked List Implementation
The main class handles insertion, deletion, and display operations ?
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 delete_from_end(self):
if self.head == None:
return
else:
if self.head != self.tail:
self.tail = self.tail.prev
self.tail.next = None
else:
self.head = self.tail = 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
# 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 elements from end one by one
while my_list.head != None:
my_list.delete_from_end()
print("The list after deleting the element from the end is:")
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 The list after deleting the element from the end is: The nodes in the doubly linked list are: 10 24 54 77 The list after deleting the element from the end is: The nodes in the doubly linked list are: 10 24 54 The list after deleting the element from the end is: The nodes in the doubly linked list are: 10 24 The list after deleting the element from the end is: The nodes in the doubly linked list are: 10 The list after deleting the element from the end is: The list is empty
How the Delete Operation Works
The delete_from_end() method handles two main scenarios:
- Single node: When head equals tail, both are set to None
- Multiple nodes: The tail is moved to the previous node, and its next pointer is set to None
Key Points
- Always check if the list is empty before deletion
- Handle the case where only one node exists
- Update both
prevandnextpointers appropriately - The tail pointer must be updated to maintain list integrity
Conclusion
Deleting from the end of a doubly linked list requires careful handling of the tail pointer and edge cases. The operation has O(1) time complexity since we maintain a direct reference to the tail node.
Advertisements
