
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to delete a new node from the end of the doubly linked list
When it is required to delete a node from the end of a doubly linked list, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.
Below is a demonstration for the same −
Example
class Node: def __init__(self, my_data): self.prev = None self.data = my_data self.next = None class double_list: 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.previous = None; self.tail.next = None; else: self.tail.next = new_node; new_node.previous = 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_end(self): if(self.head == None): return; else: if(self.head != self.tail): self.tail = self.tail.previous self.tail.next = None; else: self.head = self.tail = None; my_instance = double_list() print("Elements are being added to the doubly linked list") my_instance.add_data(10) my_instance.add_data(24) my_instance.add_data(54) my_instance.add_data(77) my_instance.add_data(92) my_instance.print_it() while(my_instance.head != None): my_instance.delete_from_end(); print("The list after deleting the element from the end is : "); my_instance.print_it();
Output
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
Explanation
- The ‘Node’ class is created.
- Another class with required attributes is created.
- A method named ‘add_data’ is defined, that is used to add data to the doubly linked list.
- Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
- Another method named ‘delete_from_end’ is defined, that deletes the node from the end, i.e the ‘tail’ node and makes the previous node as the tail node in the circular linked list.
- An object of the ‘double_list’ class is created, and the methods are called on it to delete a node from the beginning of the doubly linked list.
- An ‘init’ method is defined, that the root, head, and tail nodes of the doubly linked list to None.
- The list is iterated over, and every node from the end of the linked list is deleted until its empty.
- This is displayed on the console using the ‘print_it’ method.
- Related Articles
- Python program to delete a new node from the beginning of the doubly linked list
- Python program to delete a new node from the middle of the doubly linked list
- Python program to insert a new node at the end of the Doubly Linked List
- Python program to delete a node from the end of the Circular Linked List
- Python program to insert a new node at the beginning of the Doubly Linked list
- Python program to insert a new node at the middle of the Doubly Linked List
- Python program to insert a new node at the end of the Circular Linked List
- Python program to delete a node from the middle of the Circular Linked List
- Delete a node in a Doubly Linked List in C++
- Python program to find the maximum and minimum value node from a doubly linked list
- Golang Program to delete the first node from a linked list.
- Golang Program to delete the last node from a linked list.
- Delete a Doubly Linked List node at a given position in C++
- Program for n’th node from the end of a Linked List in C program
- Python program to insert a new node at the beginning of the Circular Linked List

Advertisements