# Reversing a Stack with the help of another empty Stack

Given a Stack consisting of N elements, the task is to reverse the Stack using an extra stack.

Examples:

Input: stack = {1, 2, 3, 4, 5}
Output:

Explanation:
Input Stack:

Reversed Stack:

5

Input: stack = {1, 3, 5, 4, 2}
Output:

2

Approach 1: Follow the steps below to solve the problem:

Below is the implementation of the above approach.

## C++

 `// C++ program to reverse a stack``// by using an extra stack``#include ``using` `namespace` `std;` `// Function to transfer elements of``// the stack s1 to the stack s2``void` `transfer(stack<``int``>& s1,``              ``stack<``int``>& s2, ``int` `n)``{``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Store the top element``        ``// in a temporary variable``        ``int` `temp = s1.top();` `        ``// Pop out of the stack``        ``s1.pop();` `        ``// Push it into s2``        ``s2.push(temp);``    ``}``}` `// Function to reverse a stack using another stack``void` `reverse_stack_by_using_extra_stack(stack<``int``>& s,``                                        ``int` `n)``{``    ``stack<``int``> s2;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Store the top element``        ``// of the given stack``        ``int` `x = s.top();` `        ``// Pop that element``        ``// out of the stack``        ``s.pop();` `        ``transfer(s, s2, n - i - 1);``        ``s.push(x);``        ``transfer(s2, s, n - i - 1);``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `n = 5;` `    ``stack<``int``> s;``    ``s.push(1);``    ``s.push(2);``    ``s.push(3);``    ``s.push(4);``    ``s.push(5);` `    ``reverse_stack_by_using_extra_stack(s, n);` `    ``for` `(``int` `i = 0; i < n; i++) {``        ``cout << s.top() << ``" "``;``        ``s.pop();``    ``}``    ``return` `0;``}`

## Java

 `// Java program to reverse a stack``// by using an extra stack``import` `java.io.*;``import` `java.util.*;` `class` `GFG{``  ` `// Function to transfer elements of``// the stack s1 to the stack s2``static` `void` `transfer(Stack s1,``                     ``Stack s2, ``int` `n)``{``    ``for``(``int` `i = ``0``; i < n; i++) ``    ``{``        ` `        ``// Store the top element``        ``// in a temporary variable``        ``int` `temp = s1.peek();` `        ``// Pop out of the stack``        ``s1.pop();` `        ``// Push it into s2``        ``s2.push(temp);``    ``}``}` `// Function to reverse a stack using another stack``static` `void` `reverse_stack_by_using_extra_stack(Stack s,``                                               ``int` `n)``{``    ``Stack s2 = ``new` `Stack();` `    ``for``(``int` `i = ``0``; i < n; i++) ``    ``{``        ` `        ``// Store the top element``        ``// of the given stack``        ``int` `x = s.peek();` `        ``// Pop that element``        ``// out of the stack``        ``s.pop();` `        ``transfer(s, s2, n - i - ``1``);``        ``s.push(x);``        ``transfer(s2, s, n - i - ``1``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `n = ``5``;` `    ``Stack s = ``new` `Stack();``    ``s.push(``1``);``    ``s.push(``2``);``    ``s.push(``3``);``    ``s.push(``4``);``    ``s.push(``5``);` `    ``reverse_stack_by_using_extra_stack(s, n);` `    ``for``(``int` `i = ``0``; i < n; i++) ``    ``{``        ``System.out.print(s.peek() + ``" "``);``        ``s.pop();``    ``}``}``}` `// This code is contributed by Dharanendra L V.`

## Python3

 `# Python3 program to reverse a stack``# by using an extra stack` `# Function to transfer elements of``# the stack s1 to the stack s2``def` `transfer(s1, s2, n):``    ` `    ``for` `i ``in` `range``(n):` `        ``# Store the top element``        ``# in a temporary variable``        ``temp ``=` `s1[``-``1``]` `        ``# Pop out of the stack``        ``s1.pop()` `        ``# Push it into s2``        ``s2.append(temp)` `# Function to reverse a stack using another stack``def` `reverse_stack_by_using_extra_stack(s, n):` `    ``s2 ``=` `[]``    ` `    ``for` `i ``in` `range``(n):` `        ``# Store the top element``        ``# of the given stack``        ``x ``=` `s[``-``1``]` `        ``# Pop that element``        ``# out of the stack``        ``s.pop()` `        ``transfer(s, s2, n ``-` `i ``-` `1``)``        ``s.append(x)``        ``transfer(s2, s, n ``-` `i ``-` `1``)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``n ``=` `5` `    ``s ``=` `[]``    ``s.append(``1``)``    ``s.append(``2``)``    ``s.append(``3``)``    ``s.append(``4``)``    ``s.append(``5``)` `    ``reverse_stack_by_using_extra_stack(s, n)` `    ``for` `i ``in` `range``(n):``        ``print``(s[``-``1``], end ``=` `" "``)``        ``s.pop()``        ` `# This code is contributed by ukasp`

## C#

 `// C# program to reverse a stack``// by using an extra stack``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to transfer elements of``// the stack s1 to the stack s2``static` `void` `transfer(Stack<``int``> s1,``              ``Stack<``int``> s2, ``int` `n)``{``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Store the top element``        ``// in a temporary variable``        ``int` `temp = s1.Peek();` `        ``// Pop out of the stack``        ``s1.Pop();` `        ``// Push it into s2``        ``s2.Push(temp);``    ``}``}` `// Function to reverse a stack using another stack``static` `void` `reverse_stack_by_using_extra_stack(Stack<``int``> s,``                                        ``int` `n)``{``    ``Stack<``int``> s2 = ``new` `Stack<``int``>();`  `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Store the top element``        ``// of the given stack``        ``int` `x = s.Peek();` `        ``// Pop that element``        ``// out of the stack``        ``s.Pop();` `        ``transfer(s, s2, n - i - 1);``        ``s.Push(x);``        ``transfer(s2, s, n - i - 1);``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``int` `n = 5;` `    ``Stack<``int``> s = ``new` `Stack<``int``>();` `    ``s.Push(1);``    ``s.Push(2);``    ``s.Push(3);``    ``s.Push(4);``    ``s.Push(5);` `    ``reverse_stack_by_using_extra_stack(s, n);` `    ``for` `(``int` `i = 0; i < n; i++) {``        ``Console.Write(s.Peek() + ``" "``);``        ``s.Pop();``    ``}``}``}` `// This code is contributed by SURENDRA_GANGWAR.`

## Javascript

 ``

Output
```1 2 3 4 5

```

Time Complexity: O(N2
Auxiliary Space: O(N)

Approach 2: Follow the steps below to solve the problem:

• Initialize an empty stack called ans.
• Pop the top element of the given stack S and push it into the stack ans.
• Return the stack ans, which will contain the reversed elements of the stack S.

Below is the implementation of the above approach.

## C++

 `#include``using` `namespace` `std;` `stack<``int``> reverse_stack_by_using_extra_stack(stack<``int``> s, ``int` `n){``    ``stack<``int``> ans;``    ``for``(``int` `i = 0; i < n; i++){``        ``ans.push(s.top());``        ``s.pop();``    ``}``    ``return` `ans;``}``// Nikunj Sonigara``int` `main()``{``    ``int` `n = 5;`` ` `    ``stack<``int``> s;``    ``s.push(1);``    ``s.push(2);``    ``s.push(3);``    ``s.push(4);``    ``s.push(5);`` ` `    ``s = reverse_stack_by_using_extra_stack(s, n);`` ` `    ``for` `(``int` `i = 0; i < n; i++) {``        ``cout << s.top() << ``" "``;``        ``s.pop();``    ``}``    ``return` `0;``}`

## Java

 `import` `java.util.Stack;` `class` `GFG {``    ``static` `Stack reverseStackByUsingExtraStack(Stack s, ``int` `n) {``        ``Stack ans = ``new` `Stack<>();``        ``for` `(``int` `i = ``0``; i < n; i++) {``            ``ans.push(s.pop());``        ``}``        ``return` `ans;``    ``}` `    ``public` `static` `void` `main(String[] args) {``        ``int` `n = ``5``;`` ` `        ``Stack s = ``new` `Stack<>();``        ``s.push(``1``);``        ``s.push(``2``);``        ``s.push(``3``);``        ``s.push(``4``);``        ``s.push(``5``);`` ` `        ``s = reverseStackByUsingExtraStack(s, n);`` ` `        ``for` `(``int` `i = ``0``; i < n; i++) {``            ``System.out.print(s.peek() + ``" "``);``            ``s.pop();``        ``}``    ``}``}`

## Python3

 `# Define a function to reverse a stack using an extra stack``def` `reverse_stack_by_using_extra_stack(s, n):``    ``ans ``=` `[]``    ``for` `i ``in` `range``(n):``        ``ans.append(s.pop())``    ``return` `ans` `# Main function``if` `__name__ ``=``=` `"__main__"``:``    ``n ``=` `5` `    ``# Create a stack and push elements into it``    ``s ``=` `[]``    ``s.append(``1``)``    ``s.append(``2``)``    ``s.append(``3``)``    ``s.append(``4``)``    ``s.append(``5``)` `    ``# Reverse the stack using the function``    ``s ``=` `reverse_stack_by_using_extra_stack(s, n)` `    ``# Print the reversed stack``    ``for` `i ``in` `range``(n):``        ``print``(s[``-``1``], end``=``" "``)``        ``s.pop()`

## C#

 `using` `System;``using` `System.Collections.Generic;` `class` `Program``{``    ``// Define a function to reverse a stack using an extra stack``    ``static` `Stack<``int``> ReverseStackUsingExtraStack(Stack<``int``> s, ``int` `n)``    ``{``        ``Stack<``int``> reversedStack = ``new` `Stack<``int``>();``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``reversedStack.Push(s.Pop());``        ``}``        ``return` `reversedStack;``    ``}` `    ``// Main function``    ``static` `void` `Main()``    ``{``        ``int` `n = 5;` `        ``// Create a stack and push elements into it``        ``Stack<``int``> s = ``new` `Stack<``int``>();``        ``s.Push(1);``        ``s.Push(2);``        ``s.Push(3);``        ``s.Push(4);``        ``s.Push(5);` `        ``// Reverse the stack using the function``        ``Stack<``int``> reversedStack = ReverseStackUsingExtraStack(s, n);` `        ``// Print the reversed stack``        ``for` `(``int` `i = 0; i < n; i++)``        ``{``            ``Console.Write(reversedStack.Peek() + ``" "``);``            ``reversedStack.Pop();``        ``}``    ``}``}` `// This code is contributed by shivamgupta0987654321`

## Javascript

 ``

Output
```1 2 3 4 5

```

Time Complexity: O(N)
Auxiliary Space: O(N)

Previous
Next