Queue Reversal

Practice this coding question on Coding Gurukul.

Problem statement

Given a queue of n integers listed from front to back, reverse the queue and print its new order from front to back. Preserve duplicates and negative values. Print EMPTY for an empty queue. Use a stack: dequeue each value and push it onto the stack, then pop and enqueue the values. Target O(n) time and O(n) extra space. Worked example The queue [5, 10, 15, 20, 25] becomes [25, 20, 15, 10, 5].

Input format

First line: n. Second line: n queue values from front to back (empty when n=0).

Output format

Print the reversed queue values separated by spaces, or EMPTY.

Constraints

0 <= n <= 100000; -10^9 <= value <= 10^9.

Sample input

5
5 10 15 20 25

Sample output

25 20 15 10 5

Sign in to run or submit a solution in C++, Java or Python.