Skip to main content

232. Implement Queue using Stacks

https://leetcode.com/problems/implement-queue-using-stacks/

Python

class MyQueue:
def __init__(self):
self.stack = []

def push(self, x: int) -> None:
self.stack.append(x)

def pop(self) -> int:
keep = []

while self.stack:
keep.append(self.stack.pop())

result = keep.pop()

while keep:
self.stack.append(keep.pop())

return result


def peek(self) -> int:
return self.stack[0]


def empty(self) -> bool:
return not bool(self.stack)