Write a Python program to print a given doubly linked list in reverse order
Python Linked List: Print a given doubly linked list in reverse orderLast update on January 04 2021 14:02:41 (UTC/GMT +8 hours)4. Python program to create a doubly linked list of n nodes and display it in reverse order.In this program, we create a doubly linked list and then reverse the list by reversing the direction of the list and print out the nodes. Show
Traverse through the list by swapping the previous pointer with next pointer of each node. Then, swap the position of head and tail node that is, head of the original list will become tail of new list and tail of the original list will become head of the new list. So, the reversed list will be: ALGORITHM:
a. reverse() will reverse the given doubly linked list.
a. display() will show all the nodes present in the list.
PROGRAM:Output: Original List:
1 2 3 4 5
Reversed List:
5 4 3 2 1
Reverse a Doubly Linked ListGiven a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked ListHere is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change the head pointer in the end. Python Program For Reversing A Doubly Linked ListGiven a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked ListHere is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes, change prev of the head (or start) and change the head pointer in the end. Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Python
Output: Original linked list 10 8 4 2 The reversed Linked List is 2 4 8 10Time Complexity: O(N), where N denotes the number of nodes in the doubly linked list. Method 2: The same question can also be done by using Stacks. Steps:
Python3
Output: Original linked list 10 8 4 2 The reversed Linked List is 2 4 8 10Time Complexity: O(N) In this method, we traverse the linked list once and add elements to the stack, and again traverse the whole for updating all the elements. The whole takes 2n time, which is the time complexity of O(n). Please refer complete article on Reverse a Doubly Linked List for more details!
Article Tags :
Linked List Python Programs
doubly linked list Practice Tags :
Linked List
Read Full Article
C++
Python program to create a doubly linked list of n nodes and display it in reverse orderPythonServer Side ProgrammingProgramming 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 − 1. Iterative SolutionThe idea is simple – Traverse the list and swap next and prev pointers for each node. Finally, update head pointer to point to the last node. Following is the C, Java, and Python program that demonstrates it: Python Linked List: Print a given doubly linked list in reverse orderLast update on January 04 2021 14:01:45 (UTC/GMT +8 hours) |