225. Implement Stack using Queues
https://leetcode.com/problems/implement-stack-using-queues/
Python
from collections import deque
class MyStack:
def __init__(self):
self.queue = deque()
self.head = None
def push(self, x: int) -> None:
self.queue.append(x)
self.head = x
def pop(self) -> int:
temp = deque()
while len(self.queue) > 1:
x = self.queue.popleft()
temp.append(x)
self.head = x
ans = self.queue.popleft()
self.queue = temp
return ans
def top(self) -> int:
return self.head
def empty(self) -> bool:
return not bool(self.queue)