Queue Reversal —
FIFO meets LIFO. Stack does the flip.
Reversing a queue uses the LIFO property of a stack to flip the FIFO order of a queue. Dequeue everything into a stack, then pop everything back into the queue. Watch the elements transfer on LearnBug and the interplay between queue and stack becomes completely clear — you'll understand both structures better for seeing them interact.
Why use a stack to reverse a queue?
A queue is FIFO — first in, first out. A stack is LIFO — last in, first out. If you dump a queue into a stack, the front of the queue goes in first and comes back out last — completely reversing the order. Then dumping the stack back into the queue gives you a reversed queue. Two O(n) passes, one auxiliary stack.
Why visualization changes everything
The direction of transfer — dequeue from left, push to stack top, pop from top, enqueue to right — is hard to track mentally. On LearnBug you see the queue and stack side by side, with elements moving between them. The reversal isn't abstract — you watch it happen physically.
Reversing queue [1, 2, 3, 4, 5]
Starting state — queue holds 1 at front
Queue: front → 1, 2, 3, 4, 5 ← rear. We want to reverse it so 5 is at front.
Queue (front→rear)
Stack (top→bottom)
Phase 1 — dequeue all into stack
Dequeue 1, push to stack. Dequeue 2, push. Continue for 3, 4, 5. Queue is now empty. Stack top is 5 — the last element dequeued.
Queue
Stack (top first)
Phase 2 — pop stack back into queue
Pop 5 → enqueue. Pop 4 → enqueue. Pop 3, 2, 1. Each pop comes out in reverse order of how they were pushed.
Queue (reversed)
Stack
Queue reversed: [5, 4, 3, 2, 1] ✓
Queue reversal — two approaches
from collections import deque
def reverse_queue(queue):
stack = []
# Phase 1: dequeue all into stack
while queue:
stack.append(queue.popleft())
# Phase 2: pop stack back into queue
while stack:
queue.append(stack.pop())
return queue
q = deque([1, 2, 3, 4, 5])
print(reverse_queue(q)) # deque([5, 4, 3, 2, 1])def reverse_queue_recursive(queue):
if not queue: # base case — empty queue
return
front = queue.popleft() # save front
reverse_queue_recursive(queue) # reverse the rest
queue.append(front) # put saved element at back
# The call stack acts as the auxiliary stack — elegant but O(n) call depthTime & Space Complexity
Frequently asked questions
Why does pushing to a stack and popping reverse order?
The first element pushed goes deepest in the stack. When you pop, the deepest element comes out last — after all elements pushed after it. This is LIFO flipping the insertion order. If you push 1, 2, 3, 4, 5, you pop 5, 4, 3, 2, 1. Push order reversed becomes pop order — that's all reversal is.
Can you reverse a queue without extra space?
Not without modifying the queue's internal structure. A queue by definition only exposes enqueue and dequeue — you can't access internal elements directly. The stack approach is the standard O(n) space solution. If you can convert the queue to a list, Python's slice list(q)[::-1] reverses in O(n) time and space as well.
How does the recursive version work without an explicit stack?
Each recursive call removes the front element, recurses on the smaller queue, then appends the saved front element to the back. By the time recursion unwinds, the first element removed (originally at the front) is the last one appended (now at the back). The call stack implicitly plays the role of the auxiliary stack.
Is this asked in interviews?
Queue reversal is a classic data structures question testing whether you understand FIFO vs LIFO and can use one to simulate the other. It's common in campus placements and DSA fundamentals interviews. More importantly, the concept extends: reversing the first k elements of a queue is a common follow-up that uses the same stack approach but only processes k elements.
How do you reverse only the first k elements of a queue?
Pop k elements into a stack, then pop the stack back into the queue (these k are now reversed). Then rotate the remaining n-k elements to the back: dequeue each one and immediately enqueue it. The result is: [reversed first k] + [original last n-k]. Time: O(n), Space: O(k).
Watch elements transfer between queue and stack
Paste your queue reversal into LearnBug and see every dequeue, push, pop, and enqueue step by step.