Implement Stack using Queues

Easy

Topics
StackQueueDesign

Implement a last-in-first-out (LIFO) stack using only queues. Implement MyStack with push(x), pop() (removes and returns the top element), top() (returns the top element), and empty().

Example 1

Input:  operations = ["MyStack","push","push","top","pop","empty"], values = [[],[1],[2],[],[],[]]
Output: [null,null,null,2,2,false]

Constraints

  • 1 <= x <= 9
  • At most 100 calls to push, pop, top, and empty.
  • pop and top are only called on a non-empty stack.
Run ⌘' · Submit ⌘⏎