Open In App

Print Reverse a linked list using Stack

Last Updated : 06 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

  1. Recursive solution to print reverse a linked list. Requires extra space.
  2. Reverse linked list and then print. This requires modifications to the original list.
  3. A O(n2) solution to print reverse of linked list that first count nodes and then prints k-th node from the end.

In this post, the efficient stack-based solution is discussed. 

  1. First, insert all the elements in the stack
  2. Print stack till stack is not empty

Note: Instead of inserting data from each node into the stack, insert the node's address onto the stack. This is because the size of the node's data will be generally more than the size of the node's address. Thus, the stack would end up requiring more memory if it directly stored the data elements. Also, we cannot insert the node's data onto the stack if each node contained more than one data member. Hence, a simpler and efficient solution would be to simply insert the node's address.

Below is the implementation of the above idea:

C++
// C/C++ program to print reverse of linked list
// using stack.
#include <bits/stdc++.h>
using namespace std;

// Link list node
struct Node {
    int data;
    struct Node* next;
};

// Given a reference (pointer to pointer) to the head
// of a list and an int,
// push a new node on the front of the list.
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->next = (*head_ref);
    (*head_ref) = new_node;
}

// Counts no. of nodes in linked list
int getCount(struct Node* head)
{
    int count = 0; // Initialize count
    struct Node* current = head; // Initialize current
    while (current != NULL) {
        count++;
        current = current->next;
    }
    return count;
}

// Takes head pointer of the linked list and index
// as arguments and return data at index
int getNth(struct Node* head, int n)
{
    struct Node* curr = head;
    for (int i = 0; i < n - 1 && curr != NULL; i++)
        curr = curr->next;
    return curr->data;
}

void printReverse(Node* head)
{
    // store Node addresses in stack
    stack<Node*> stk;
    Node* ptr = head;
    while (ptr != NULL) {
        stk.push(ptr);
        ptr = ptr->next;
    }

    // print data from stack
    while (!stk.empty()) {
        cout << stk.top()->data << " ";
        stk.pop(); // pop after print
    }
    cout << "\n";
}

// Driver code
int main()
{
    // Start with the empty list
    struct Node* head = NULL;

    // Use push() to construct below list
    // 1->2->3->4->5 
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);

    // Function call
    printReverse(head);

    return 0;
}
Java Python3 C# JavaScript

Output
5 4 3 2 1 

Time complexity: O(N) where N is number of nodes of linked list
Auxiliary Space: O(n)


Next Article
Article Tags :
Practice Tags :

Similar Reads