Reverse a stack with an array

Pop everything once and the order flips. Watch each value fly from the stack into the array.

A stack only releases its top, so draining it replays history backwards. Pop each value and write it into an array, and by the time the stack is empty the array holds everything in reverse order.

One pass, no swaps, no index math. This is why undo history replays your latest action first, and why a single stack pass is the cheapest way to reverse any sequence.

Remember this

  • Draining a stack reverses any sequence in one pass
  • Each pop takes the newest remaining value first (LIFO)
  • No extra logic needed: the structure does the reversing

Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.

Ask your own question →

Keep learning