# Print Reverse a linked list using Stack

Given a linked list, print 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
```

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->NULL
Output :5->4->3->2->1->NULL

Input :10->5->15->20->24->NULL
Output :24->20->15->5->10->NULL
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Below are different solutions that are now allowed here as we cannot use extra space and modify list.

1) Recursive solution to print reverse a linked list. Requires extra space.

2) Reverse linked list and then print. This requires modifications to original list.

3) A O(n2) solution to print reverse of linked list that first count nodes and then prints k-th node from end.

In this post, an efficient stack based solution is discussed.
1. First insert all the element in 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 the simpler and efficient solution would be to simply insert the node’s address.

## C++

 `// C/C++ program to print reverse of linked list ` `// using stack. ` `#include ` `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; inext; ` `    ``return` `curr->data; ` `} ` `  `  `void` `printReverse(Node *head) ` `{ ` `    ``// store Node addresses in stack ` `    ``stack 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 program to test count function*/` `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); ` `  `  `    ``printReverse(head); ` `  `  `    ``return` `0; ` `} `

## Java

 `// Java program to print reverse of linked list ` `// using stack. ` `import` `java.util.*; ` ` `  `class` `GFG ` `{ ` `     `  `/* Link list node */` `static` `class` `Node ` `{ ` `    ``int` `data; ` `    ``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. */` `static` `Node push( Node head_ref, ``int` `new_data) ` `{ ` `    ``Node new_node = ``new` `Node(); ` `    ``new_node.data = new_data; ` `    ``new_node.next = (head_ref); ` `    ``(head_ref) = new_node; ` `    ``return` `head_ref; ` `} ` ` `  `/* Counts no. of nodes in linked list */` `static` `int` `getCount( Node head) ` `{ ` `    ``int` `count = ``0``; ``// Initialize count ` `    ``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*/` `static` `int` `getNth( Node head, ``int` `n) ` `{ ` `    ``Node curr = head; ` `    ``for` `(``int` `i = ``0``; i < n-``1` `&& curr != ``null``; i++) ` `        ``curr = curr.next; ` `    ``return` `curr.data; ` `} ` ` `  `static` `void` `printReverse(Node head) ` `{ ` `    ``// store Node addresses in stack ` `    ``Stack stk = ``new` `Stack();  ` `    ``Node ptr = head; ` `    ``while` `(ptr != ``null``)  ` `    ``{ ` `        ``stk.push(ptr); ` `        ``ptr = ptr.next; ` `    ``} ` ` `  `    ``// print data from stack ` `    ``while` `(stk.size() > ``0``) ` `    ``{ ` `        ``System.out.print( stk.peek().data + ``" "``); ` `        ``stk.pop(); ``// pop after print ` `    ``} ` `    ``System.out.println( ``"\n"``); ` `} ` ` `  `/* Driver code*/` `public` `static` `void` `main(String args[]) ` `{ ` `    ``/* Start with the empty list */` `    ``Node head = ``null``; ` ` `  `    ``/* Use push() to con below list ` `    ``1.2.3.4.5 */` `    ``head=push(head, ``5``); ` `    ``head=push(head, ``4``); ` `    ``head=push(head, ``3``); ` `    ``head=push(head, ``2``); ` `    ``head=push(head, ``1``); ` ` `  `    ``printReverse(head); ` `} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

## Python3

 `# Python3 program to print reverse of linked list ` `# using stack. ` ` `  `# Node of a linked list  ` `class` `Node:  ` `    ``def` `__init__(``self``, ``next` `=` `None``, data ``=` `None``):  ` `        ``self``.``next` `=` `next` `        ``self``.data ``=` `data  ` ` `  `# 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.  ` `def` `push( head_ref, new_data): ` ` `  `    ``new_node ``=` `Node() ` `    ``new_node.data ``=` `new_data ` `    ``new_node.``next` `=` `(head_ref) ` `    ``(head_ref) ``=` `new_node ` `    ``return` `head_ref ` ` `  `# Counts no. of nodes in linked list  ` `def` `getCount( head): ` ` `  `    ``count ``=` `0` `# Initialize count ` `    ``current ``=` `head ``# Initialize current ` `    ``while` `(current !``=` `None``): ` `     `  `        ``count ``=` `count ``+` `1` `        ``current ``=` `current.``next` `     `  `    ``return` `count ` ` `  `# Takes head pointer of the linked list and index ` `# as arguments and return data at index ` `def` `getNth( head, n): ` ` `  `    ``curr ``=` `head ` `    ``i ``=` `0` `    ``while``( i < n ``-` `1` `and` `curr !``=` `None` `): ` `        ``curr ``=` `curr.``next` `        ``i ``=` `i ``+` `1` `    ``return` `curr.data ` ` `  `def` `printReverse(head): ` ` `  `    ``# store Node addresses in stack ` `    ``stk ``=` `[] ` `    ``ptr ``=` `head ` `    ``while` `(ptr !``=` `None``):  ` `     `  `        ``stk.append(ptr) ` `        ``ptr ``=` `ptr.``next` `     `  `    ``# print data from stack ` `    ``while` `(``len``(stk) > ``0``): ` `     `  `        ``print``( stk[``-``1``].data, end ``=` `" "``) ` `        ``stk.pop() ``# pop after print ` `     `  `    ``print``( ``" "``) ` ` `  `# Driver code ` ` `  `# Start with the empty list  ` `head ``=` `None` ` `  `# Use push() to con below list ` `# 1.2.3.4.5  ` `head ``=` `push(head, ``5``) ` `head ``=` `push(head, ``4``) ` `head ``=` `push(head, ``3``) ` `head ``=` `push(head, ``2``) ` `head ``=` `push(head, ``1``) ` ` `  `printReverse(head) ` ` `  `# This code is Contributed by Arnab Kundu `

## C#

 `// C# program to print reverse of linked list ` `// using stack.  ` `using` `System; ` `using` `System.Collections.Generic;  ` ` `  `class` `GFG ` `{ ` `     `  `/* Link list node */` `public` `class` `Node ` `{ ` `    ``public` `int` `data; ` `    ``public` `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. */` `static` `Node push( Node head_ref, ``int` `new_data) ` `{ ` `    ``Node new_node = ``new` `Node(); ` `    ``new_node.data = new_data; ` `    ``new_node.next = (head_ref); ` `    ``(head_ref) = new_node; ` `    ``return` `head_ref; ` `} ` ` `  `/* Counts no. of nodes in linked list */` `static` `int` `getCount( Node head) ` `{ ` `    ``int` `count = 0; ``// Initialize count ` `    ``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*/` `static` `int` `getNth( Node head, ``int` `n) ` `{ ` `    ``Node curr = head; ` `    ``for` `(``int` `i = 0; i < n-1 && curr != ``null``; i++) ` `        ``curr = curr.next; ` `    ``return` `curr.data; ` `} ` ` `  `static` `void` `printReverse(Node head) ` `{ ` `    ``// store Node addresses in stack ` `    ``Stack stk = ``new` `Stack();  ` `    ``Node ptr = head; ` `    ``while` `(ptr != ``null``)  ` `    ``{ ` `        ``stk.Push(ptr); ` `        ``ptr = ptr.next; ` `    ``} ` ` `  `    ``// print data from stack ` `    ``while` `(stk.Count > 0) ` `    ``{ ` `        ``Console.Write( stk.Peek().data + ``" "``); ` `        ``stk.Pop(); ``// pop after print ` `    ``} ` `    ``Console.WriteLine( ``"\n"``); ` `} ` ` `  `/* Driver code*/` `public` `static` `void` `Main(String []args) ` `{ ` `    ``/* Start with the empty list */` `    ``Node head = ``null``; ` ` `  `    ``/* Use push() to con below list ` `    ``1.2.3.4.5 */` `    ``head=push(head, 5); ` `    ``head=push(head, 4); ` `    ``head=push(head, 3); ` `    ``head=push(head, 2); ` `    ``head=push(head, 1); ` ` `  `    ``printReverse(head); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```5 4 3 2 1
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.