Queue Reversal
easySave
QueueRecursionStack
Given a queue of integers, write a function to reverse the order of its elements. For example, if the input queue has the elements [1, 2, 3, 4, 5], after reversing, the queue should have the elements [5, 4, 3, 2, 1].
Example 1
Input
[ 1, 2, 3, 4, 5 ]
Output
[5, 4, 3, 2, 1]
Explanation. Simply reversing the queue of [1, 2, 3, 4, 5] results in [5, 4, 3, 2, 1].
Example 2
Input
[ 10, -1, 20, 4, 0 ]
Output
[0, 4, 20, -1, 10]
Explanation. Reversing the queue of [10, -1, 20, 4, 0] results in [0, 4, 20, -1, 10].
Example 3
Input
[ 9 ]
Output
[9]
Explanation. A queue with a single element remains the same after reversal.
Follow-up: How would you handle the scenario if the queue data structure does not support indexing or random access?
Constraints:
The given queue will contain at least 1 element and at most 1000 elements. Elements could be any valid integers.
- Views
- 1