![]() |
VOOZH | about |
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:
1
2
3
4
5
Explanation:
Input Stack:
5
4
3
2
1
Reversed Stack:
1
2
3
4
5Input: stack = {1, 3, 5, 4, 2}
Output:
1
3
5
4
2
Approach 1: Follow the steps below to solve the problem:
Below is the implementation of the above approach.
1 2 3 4 5
Time Complexity: O(N2)
Auxiliary Space: O(N)
Approach 2: Follow the steps below to solve the problem:
Below is the implementation of the above approach.
1 2 3 4 5
Time Complexity: O(N)
Auxiliary Space: O(N)