Print reverse of a Linked List without actually reversing
Last Updated :
30 Aug, 2024
Given a singly linked list. The task is to print the linked list in reverse order without actually reversing the linked list.
Examples:
Input: head : 1 -> 2 -> 3 -> 4 -> NULL
Output: 4 -> 3 -> 2 -> 1 -> NULL
Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output: 5 -> 4 -> 3 -> 2 -> 1 -> NULL
[Expected Approach - 1] Using Recursion – O(n) Time and O(n) Space:
The idea is to use recursion to print the linked list in reverse order. Note that the question is only about printing the reverse , to reverse actual list please see this.
Below is the implementation of the above approach:
C++
// C++ program to print reverse of a linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function to print the linked
// list in reverse order
void printReverse(Node *curr) {
// Base case
if (curr == nullptr)
return;
// Recursively move to the next node
printReverse(curr->next);
// Print the data after recursion
cout << curr->data << " ";
}
int main() {
// Creating the linked list 1->2->3->4
Node *head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
printReverse(head);
return 0;
}
C
// C program to print reverse of a linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to reverse the linked list
void printReverse(struct Node* curr) {
// Base case
if (curr == NULL)
return;
// print the list after head node
printReverse(curr->next);
// After everything else is printed, print head
printf("%d ", curr->data);
}
struct Node* createNode(int x) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = NULL;
return newNode;
}
int main() {
// Let us create linked list 1->2->3->4
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
printReverse(head);
return 0;
}
Java
// Java program to print reverse of a linked list
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to reverse the linked list
static void printReverse(Node curr) {
// Base case
if (curr == null)
return;
// Print the list after head node
printReverse(curr.next);
// After everything else is
// printed, print head
System.out.print(curr.data + " ");
}
public static void main(String[] args) {
// Let us create linked list 1->2->3->4
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
printReverse(head);
}
}
Python
# Python3 program to print reverse
# of a linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to reverse the linked list
def print_reverse(curr):
# Base case
if curr is None:
return
# print the list after head node
print_reverse(curr.next)
# After everything else is printed, print head
print(curr.data, end=" ")
# Let us create linked list 1->2->3->4
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
print_reverse(head)
C#
// C# program to print reverse of a linked list
using System;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to reverse the linked list
static void PrintReverse(Node curr) {
// Base case
if (curr == null)
return;
// Print the list after head node
PrintReverse(curr.next);
// After everything else is printed, print head
Console.Write(curr.data + " ");
}
static void Main(String[] args) {
// Let us create linked list 1->2->3->4
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
PrintReverse(head);
}
}
JavaScript
// Javascript program to print
// reverse of a linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to reverse the linked list
function printReverse(curr) {
// Base case
if (curr === null) {
return;
}
// Print the list after head node
printReverse(curr.next);
// After everything else is printed, print head
console.log(curr.data + " ");
}
// Let us create linked list 1->2->3->4
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
printReverse(head);
Time Complexity: O(n) , Where n is the number of nodes.
Auxiliary Space: O(n)
[Expected Approach - 2] Using Stack – O(n) Time and O(n) Space:
The idea is similar to the recursion , we can also perform printing in reverse order using a stack (iterative method). Store the values of the linked list in a stack. After traversing keep removing the elements from the stack and print them.
Below is the implementation of above approach:
C++
// C++ program to print reverse
// of a linked list
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function to print the linked list in
// reverse order using a stack
void printReverse(Node *head) {
// Initialize a stack to store the node data
stack<int> st;
Node *curr = head;
// Traverse the linked list and push data
// of each node onto the stack
while (curr != nullptr) {
st.push(curr->data);
curr = curr->next;
}
// Pop and print each element from the
// stack to get the reverse order
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
}
int main() {
// Creating the linked list 1->2->3->4
Node *head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
head->next->next->next = new Node(4);
printReverse(head);
return 0;
}
Java
// Java program to print reverse of a linked
import java.util.Stack;
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to print the linked list
// in reverse order using a stack
static void printReverse(Node head) {
// Initialize a stack to store the node data
Stack<Integer> st = new Stack<>();
Node curr = head;
// Traverse the linked list and push
// data of each node onto the stack
while (curr != null) {
st.push(curr.data);
curr = curr.next;
}
// Pop and print each element from
// the stack to get the reverse order
while (!st.isEmpty()) {
System.out.print(st.pop() + " ");
}
}
public static void main(String[] args) {
// Let us create linked list 1->2->3->4
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
printReverse(head);
}
}
Python
# Python3 program to print reverse
# of a linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
def print_reverse(head):
# Initialize a stack to store the node data
st = []
curr = head
# Traverse the linked list and
# push data of each node onto the stack
while curr:
st.append(curr.data)
curr = curr.next
# Pop and print each element
# from the stack to get the reverse order
while st:
print(st.pop(), end=" ")
# Let us create linked list 1->2->3->4
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
print_reverse(head)
C#
// C# program to print reverse of a linked list
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to print the linked
// list in reverse order using a stack
static void printReverse(Node head) {
// Initialize a stack to store the node data
Stack<int> st = new Stack<int>();
Node curr = head;
// Traverse the linked list and
// push data of each node onto the stack
while (curr != null) {
st.Push(curr.data);
curr = curr.next;
}
// Pop and print each element
// from the stack to get the reverse order
while (st.Count > 0) {
Console.Write(st.Pop() + " ");
}
}
public static void Main(String[] args) {
// Let us create linked list 1->2->3->4
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
printReverse(head);
}
}
JavaScript
// Javascript program to print reverse
// of a linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function printReverse(head) {
// Initialize a stack to store the node data
let stack = [];
let curr = head;
// Traverse the linked list and
// push data of each node onto the stack
while (curr !== null) {
stack.push(curr.data);
curr = curr.next;
}
// Pop and print each element
// from the stack to get the reverse order
while (stack.length > 0) {
console.log(stack.pop() + " ");
}
}
// Let us create linked list 1->2->3->4
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
printReverse(head);
Time Complexity: O(n) , As we are traversing the linked list only once.
Auxiliary Space: O(n)
Similar Reads
Javascript Program For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit
2 min read
Reverse a Doubly Linked List without swapping nodes Write a program to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Approach: In the previous post, doubly linked list is being reversed by swapping prev and next pointers for all nodes, changing prev of the head (o
10 min read
Print reverse of a Linked List without extra space and modifications Given a Linked List, display the linked list in reverse without using recursion, stack or modifications to given list. Examples: Input: 1->2->3->4->5->NULLOutput: 5 4 3 2 1 Input: 10->5->15->20->24->NULLOutput: 24 20 15 5 10 Below are different solutions that are now al
7 min read
Reverse all the word in a String represented as a Linked List Given a Linked List which represents a sentence S such that each node represents a letter, the task is to reverse the sentence without reversing individual words. For example, for a given sentence "I love Geeks for Geeks", the Linked List representation is given as: I-> ->l->o->v->e-
15 min read
Print Reverse a linked list using Stack Given a linked list, print the reverse of it without modifying the list. Examples: Input : 1 2 3 4 5 6 Output : 6 5 4 3 2 1 Input : 12 23 34 45 56 67 78 Output : 78 67 56 45 34 23 12 Below are different solutions that are now allowed here as we cannot use extra space and modify the list. Recursive s
9 min read
Reverse a Doubly linked list using recursion Given a doubly linked list. Reverse it using recursion. Original Doubly linked list Reversed Doubly linked list We have discussed Iterative solution to reverse a Doubly Linked List Algorithm: If list is empty, return Reverse head by swapping head->prev and head->next If prev = NULL it means th
9 min read