Print Doubly Linked list in Reverse Order
Last Updated :
11 Jan, 2023
Given a doubly-linked list of positive integers. The task is to print the given doubly linked list data in reverse order.
Examples:
Input: List = 1 <=> 2 <=> 3 <=> 4 <=> 5
Output: 5 4 3 2 1
Input: 10 <=> 20 <=> 30 <=> 40
Output: 40 30 20 10
Approach:
- Take a pointer to point to head of the doubly linked list.
- Now, start traversing through the linked list till the end.
- After reaching last node, start traversing in backward direction and simultaneously print the node->data.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void reversePrint( struct Node** head_ref)
{
struct Node* tail = *head_ref;
while (tail->next != NULL) {
tail = tail->next;
}
while (tail != *head_ref) {
cout << tail->data << " " ;
tail = tail->prev;
}
cout << tail->data << endl;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
push(&head, 2);
push(&head, 4);
push(&head, 8);
push(&head, 10);
cout << "Linked List elements in reverse order : " << endl;
reversePrint(&head);
return 0;
}
|
Java
public class Sol
{
static class Node
{
int data;
Node next;
Node prev;
};
static void reversePrint( Node head_ref)
{
Node tail = head_ref;
while (tail.next != null )
{
tail = tail.next;
}
while (tail != head_ref)
{
System.out.print( tail.data + " " );
tail = tail.prev;
}
System.out.println( tail.data );
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 2 );
head = push(head, 4 );
head = push(head, 8 );
head = push(head, 10 );
System.out.print( "Linked List elements in reverse order : " );
reversePrint(head);
}
}
|
Python3
class node:
def __init__( self ,data = 0 ):
self .data = 0
self . next = 0
self .prev = 0
class DLL():
def __init__( self ):
self .head = node()
def create( self ,data):
if self .head.data is 0 :
self .head.data = data
self .head. next = 0
self .head.prev = 0
elif self .head. next is 0 :
newnode = node()
self .head. next = newnode
newnode.prev = self .head
newnode.data = data
newnode. next = 0
else :
newnode = node()
curr = self .head
while curr. next :
curr = curr. next
curr. next = newnode
newnode.prev = curr
newnode.data = data
newnode. next = 0
def back( self ):
cursor = self .head
while cursor! = 0 :
copy = cursor
cursor = cursor. next
print ( "Reverse of Doubly Linked list:-" )
while copy! = self .head.prev:
print (copy.data)
copy = copy.prev
def printlist( self ):
cursor = self .head
while cursor! = 0 :
print (cursor.data)
cursor = cursor. next
ll = DLL()
ll.create( 2 )
ll.create( 4 )
ll.create( 8 )
ll.create( 10 )
ll.back()
|
C#
using System;
class Sol
{
public class Node
{
public int data;
public Node next;
public Node prev;
};
static void reversePrint( Node head_ref)
{
Node tail = head_ref;
while (tail.next != null )
{
tail = tail.next;
}
while (tail != head_ref)
{
Console.Write( tail.data + " " );
tail = tail.prev;
}
Console.WriteLine( tail.data );
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 2);
head = push(head, 4);
head = push(head, 8);
head = push(head, 10);
Console.Write( "Linked List elements in reverse order : " );
reversePrint(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
this .prev = null ;
}
}
function reversePrint(head_ref) {
var tail = head_ref;
while (tail.next != null ) {
tail = tail.next;
}
while (tail != head_ref) {
document.write(tail.data + " " );
tail = tail.prev;
}
document.write(tail.data + "<br>" );
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = head_ref;
if (head_ref != null ) head_ref.prev = new_node;
head_ref = new_node;
return head_ref;
}
var head = null ;
head = push(head, 2);
head = push(head, 4);
head = push(head, 8);
head = push(head, 10);
document.write( "Linked List elements in reverse order : <br>" );
reversePrint(head);
</script>
|
Output
Linked List elements in reverse order :
2 4 8 10
Complexity Analysis:
- Time complexity: O(N) where N is no of nodes in Doubly Linked List
- Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...