Print reverse of a Linked List without actually reversing (original) (raw)

Last Updated : 23 Jul, 2025

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

Table of Content

**[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 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)