Reverse an array using Stack
Last Updated :
11 Jun, 2025
Given an array arr[] of size n, the task is to reverse the array using Stack.
Examples:
Input: arr = [10, 20, 30, 40, 50]
Output: 50 40 30 20 10
Explanation: Upon reversing the array becomes [50, 40, 30, 20, 10]. Therefore, the output is 50 40 30 20 10.
Input: arr = [ 1 ]
Output: 1
Explanation: Reversing the array does not changes the array as it only has a single element
Also read: Array Reverse – Complete Tutorial
The idea is to push all elements of the array into the stack. Since a stack follows the Last In, First Out (LIFO) principle, popping elements from the stack and storing them back into the array results in a reversed order. This efficiently reverses the array without using extra loops for swapping.
Follow the steps given below to reverse an array using stack.
- Create an empty stack.
- One by one push all elements of the array to stack.
- One by one pop all elements from the stack and push them back to the array.
C++
#include <bits/stdc++.h>
using namespace std;
void reverseArray(vector<int> &arr)
{
int n = arr.size();
//declare a stack
stack<int> st;
// push all elements of array into stack
for (int i = 0; i < n; i++)
{
st.push(arr[i]);
}
int i = 0;
// update the elements of the array
while (!st.empty())
{
int top = st.top();
st.pop();
arr[i] = top;
i++;
}
return;
}
int main()
{
vector<int> arr = {100, 200, 300, 400};
reverseArray(arr);
for (auto it : arr)
cout << it << " ";
return 0;
}
Java
import java.util.*;
class GfG {
static void reverseArray(int arr[])
{
int n = arr.length;
//declare a stack
Stack<Integer> st = new Stack<>();
//push array elements into stack
for (int i = 0; i < n; i++) {
st.push(arr[i]);
}
// update elements of array
int i = 0;
while (st.empty() == false) {
int top = st.peek();
st.pop();
arr[i] = top;
i++;
}
return;
}
public static void main(String args[])
{
int[] a = new int[] { 100, 200, 300, 400 };
reverseArray(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
Python
def reverseArray(arr):
n = len(arr)
st = []
# push all elements of array into stack
for i in range(n):
st.append(arr[i])
# update the elements of array
i = 0
while(len(st) > 0):
top = st.pop()
arr[i] = top
i += 1
n = 4
arr = [100, 200, 300, 400]
reverseArray(arr)
for i in range(n):
print(arr[i], end=" ")
C#
using System;
using System.Collections.Generic;
class GfG {
public static void reverseArray(int[] arr)
{
int n = arr.Length;
Stack<int> st = new Stack<int>();
// pushing array elements into stack
for (int i = 0; i < n; i++) {
st.Push(arr[i]);
}
// Reverse the array elements
for (int i = 0; i < n; i++) {
int top = st.Peek();
st.Pop();
arr[i] = top;
}
return;
}
public static void Main()
{
int n = 4;
int[] a = new int[] { 100, 200, 300, 400 };
reverseArray(a);
for (int i = 0; i < n; i++) {
Console.Write(a[i] + " ");
}
}
}
JavaScript
function reverseArray(arr)
{
let n = arr.length;
// Declare a stack
const st = [];
// push all elements of array into stack
for (let i = 0; i < n; i++) {
st.push(arr[i]);
}
let i = 0;
//updating elements of array
while (st.length) {
// get top element of stack and pop it
const top = st.pop();
// update ith index of array and move to next index
arr[i] = top;
i++;
}
return;
}
const n = 4;
const arr = [ 100, 200, 300, 400 ];
reverseArray(arr);
console.log(arr.join(" "))
Similar Reads
Reverse a Stack using Queue Given a stack, the task is to reverse the stack using the queue data structure. Examples: Input: Stack: (Top to Bottom) [10 -> 20 -> 30 -> 40]Output: Stack: (Top to Bottom) [40 -> 30 -> 20 -> 10] Input: Stack: [6 -> 5 -> 4]Output: Stack: [4 -> 5 -> 6] Approach: The prob
5 min read
Reverse a String using Stack Given a string str, the task is to reverse it using stack. Example:Input: s = "GeeksQuiz"Output: ziuQskeeGInput: s = "abc"Output: cbaAlso read: Reverse a String â Complete Tutorial.As we all know, stacks work on the principle of first in, last out. After popping all the elements and placing them bac
3 min read
Implement Stack using Array Stack is a linear data structure which follows LIFO principle. To implement a stack using an array, initialize an array and treat its end as the stackâs top. Implement push (add to end), pop (remove from end), and peek (check end) operations, handling cases for an empty or full stack.Step-by-step ap
10 min read
How to Reverse a Stack using Recursion Write a program to reverse a stack using recursion, without using any loop.Example: Input: elements present in stack from top to bottom 4 3 2 1Output: 1 2 3 4Input: elements present in stack from top to bottom 1 2 3Output: 3 2 1The idea of the solution is to hold all values in Function Call Stack un
4 min read
Print Reverse a linked list using Stack 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. Recursive s
9 min read