- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to create a doubly linked list of n nodes and display it in reverse order
When it is required to create a doubly linked list and display the elements in a reversed order, 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.
Another class needs to be created that would have an initialization function, and the head of the node would be initialized to ‘None’ inside this.
Multiple methods are defined by the user to add node to the linked list, to reverse the nodes and print the nodes in the linked list.
Below is a demonstration for the same −
class Node: def __init__(self, my_data): self.prev = None self.data = my_data self.next = None class reverse_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 reverse_vals(self): curr = self.head; while(curr != None): temp = curr.next curr.next = curr.previous curr.previous = temp curr = curr.previous temp = self.head self.head = self.tail self.tail = temp def print_it(self): curr = self.head if (self.head == None): print("The list is empty") return print("The nodes are :") while curr != None: print(curr.data) curr = curr.next my_instance = reverse_list() print("Elements are being added to the list") my_instance.add_data(10) my_instance.add_data(14) my_instance.add_data(24) my_instance.add_data(17) my_instance.add_data(22) my_instance.print_it() print("The reversed nodes in the doubly linked list are : ") my_instance.reverse_vals() my_instance.print_it()
Elements are being added to the list The nodes are : 10 14 24 17 22 The reversed nodes in the doubly linked list are : The nodes are : 22 17 24 14 10
- 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 ‘reverse_node’ is defined, that helps in reverse the order of nodes in the doubly linked list.
- Another method named ‘print_it’ is defined, that displays the nodes of the circular linked list.
- An object of the ‘reverse_list’ class is created, and the methods are called on it to reverse the nodes 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 ‘reverse_vals’ method is called.
- It iterates through the doubly linked list, and reverse the list.
- This is displayed on the console using the ‘print_it’ method.
- Python program to create a Circular Linked List of n nodes and display it in reverse order
- Python program to create a doubly linked list of n nodes and count the number of nodes
- Python program to create and display a doubly linked list
- Python program to rotate doubly linked list by N nodes
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Program to reverse inner nodes of a linked list in python
- Python program to create a Circular Linked List of N nodes and count the number of nodes
- C program to display numbers in reverse order using single linked list
- Golang program to count the number of nodes in a doubly linked list.
- Python program to create and display a Circular Linked List
- Program to delete n nodes after m nodes from a linked list in Python
- Python program to create a doubly linked list from a ternary tree
- Reverse a Doubly Linked List using C++
- Python Program to Reverse only First N Elements of a Linked List