232. Implement queue using stacks

232. Implement queue using stacks

[Easy] “Implement a queue using stacks.."

Link to Leetcode

Python3:

class MyQueue:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.stack_1 = []
        self.stack_2 = []

    def push(self, x: int) -> None:
        """
        Push element x to the back of queue.
        """
        self.stack_1.append(x)

    def pop(self) -> int:
        """
        Removes the element from in front of queue and returns that element.
        """
        for _ in range(len(self.stack_1)):
            stack_pop = self.stack_1.pop()
            self.stack_2.append(stack_pop)
        
        res = self.stack_2.pop()
        
        for _ in range(len(self.stack_2)):
            stack_pop = self.stack_2.pop()
            self.stack_1.append(stack_pop)
        
        return res
        

    def peek(self) -> int:
        """
        Get the front element.
        """
        return self.stack_1[0]
        

    def empty(self) -> bool:
        """
        Returns whether the queue is empty.
        """
        return not self.stack_1
        


# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()