We can implement this using a single queue. When pushing an element, we add it to the queue and then rotate the queue (pop and re-append) n-1 times so that the newest element is at the front.
push(x):- Add
xto the queue. - For
size - 1times, dequeue the front element and enqueue it again.
- Add
pop(): Dequeue from the queue.top(): Peek at the front of the queue.empty(): Check if queue is empty.
- Time Complexity:
- Push: O(N)
- Pop: O(1)
- Space Complexity: O(N).
from collections import deque
class MyStack:
def __init__(self):
self.queue = deque()
def push(self, x: int) -> None:
self.queue.append(x)
for _ in range(len(self.queue) - 1):
self.queue.append(self.queue.popleft())
def pop(self) -> int:
return self.queue.popleft()
def top(self) -> int:
return self.queue[0]
def empty(self) -> bool:
return not self.queue