Stack Using Single Queue
Implement a stack using a single queue. Your implementation should support the following operations:
push(value)- Push element onto stackpop()- Remove element from stacktop()- Get the top elementisEmpty()- Check if stack is empty
A single queue cannot make push, pop, and top all O(1). Each valid approach below chooses which operations are O(1) and which take O(n), while preserving LIFO semantics.
Examples
Input: [["Solution","push","push","pop","top"],[[],[1],[2],[],[]]]
Output: [null,null,null,2,1]
Input: [["Solution","pop"],[[],[]]]
Output: [null,-1]
Hints
Use a single queue.
On push, rotate the queue to move new element to front.
Push new element, then rotate (size-1) times to bring it to front.
Stack Using Single Queue
Implement a stack using a single queue. Your implementation should support the following operations: