Python program to create a doubly linked list of n nodes and display it in reverse order.

Created with Sketch.

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.

python program to create a doubly linked list of n nodes and display it in reverse order

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:

python program to create a doubly linked list of n nodes and display it in reverse order

ALGORITHM:

  1. Define a Node class which represents a node in the list. It will have three properties: data, previous which will point to the previous node and next which will point to the next node.
  2. Define another class for creating a doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
  3. addNode() will add node to the list:
  • It first checks whether the head is null, then it will insert the node as the head.
  • Both head and tail will point to a newly added node.
  • Head’s previous pointer will point to null and tail’s next pointer will point to null.
  • If the head is not null, the new node will be inserted at the end of the list such that new node’s previous pointer will point to tail.
  • The new node will become the new tail. Tail’s next pointer will point to null.

a. reverse() will reverse the given doubly linked list.

  • Define a node current which will initially point to head.
  • Traverse through the list by making current to point to current.next in each iteration till current points to null.
  • In each iteration, swap previous and next pointer of each node to reverse the direction of the list.
  • In the end, swap the position of head and tail.

a. display() will show all the nodes present in the list.

  • Define a new node ‘current’ that will point to the head.
  • Print current.data till current points to null.
  • Current will point to the next node in the list in each iteration.

PROGRAM:

  1. #Represent a node of doubly linked list  
  2. class Node:
  3.     def __init__(self,data):
  4.         self.data = data;
  5.         self.previous = None;
  6.         self.next = None;
  7. class ReverseList:
  8.     #Represent the head and tail of the doubly linked list  
  9.     def __init__(self):
  10.         self.head = None;
  11.         self.tail = None;
  12.     #addNode() will add a node to the list  
  13.     def addNode(self, data):
  14.         #Create a new node  
  15.         newNode = Node(data);
  16.         #If list is empty  
  17.         if(self.head == None):
  18.             #Both head and tail will point to newNode  
  19.             self.head = self.tail = newNode;
  20.             #head’s previous will point to None  
  21.             self.head.previous = None;
  22.             #tail’s next will point to None, as it is the last node of the list  
  23.             self.tail.next = None;
  24.         else:
  25.             #newNode will be added after tail such that tail’s next will point to newNode  
  26.             self.tail.next = newNode;
  27.             #newNode’s previous will point to tail  
  28.             newNode.previous = self.tail;
  29.             #newNode will become new tail  
  30.             self.tail = newNode;
  31.             #As it is last node, tail’s next will point to None  
  32.             self.tail.next = None;
  33.     #reverse() will reverse the doubly linked list  
  34.     def reverse(self):
  35.         #Node current will point to head  
  36.         current = self.head;
  37.         #Swap the previous and next pointers of each node to reverse the direction of the list  
  38.         while(current != None):
  39.             temp = current.next;
  40.             current.next = current.previous;
  41.             current.previous = temp;
  42.             current = current.previous;
  43.         #Swap the head and tail pointers.  
  44.         temp = self.head;
  45.         self.head = self.tail;
  46.         self.tail = temp;
  47.     #display() will print out the elements of the list  
  48.     def display(self):
  49.         #Node current will point to head  
  50.         current = self.head;
  51.         if(self.head == None):
  52.             print(“List is empty”);
  53.             return;
  54.         while(current != None):
  55.             #Prints each node by incrementing pointer.  
  56.             print(current.data),
  57.             current = current.next;
  58. dList = ReverseList();
  59. #Add nodes to the list  
  60. dList.addNode(1);
  61. dList.addNode(2);
  62. dList.addNode(3);
  63. dList.addNode(4);
  64. dList.addNode(5);
  65. print(“Original List: “);
  66. dList.display();
  67. #Reverse the given list  
  68. dList.reverse();
  69. #Displays the reversed list  
  70. print(“\nReversed List: “);
  71. dList.display();

Output:

Original List: 
1 2 3 4 5 
Reversed List: 
5 4 3 2 1

Leave a Reply

Your email address will not be published. Required fields are marked *