LeetCode 232 Implement Queue using Stacks

LeetCode 232 Implement Queue using Stacks Problem

Download Code
# class Queue(object):
#     def __init__(self):
#         """
#         initialize your data structure here.
#         """
#         self.stack1 = []
#         self.stack2 = []
#
#
#     def push(self, x):
#         """
#         :type x: int
#         :rtype: nothing
#         """
#         while len(self.stack1) > 0:
#             curr = self.stack1.pop()
#             self.stack2.append(curr)
#         self.stack1.append(x)
#         while len(self.stack2) > 0:
#             curr = self.stack2.pop()
#             self.stack1.append(curr)
#
#     def pop(self):
#         """
#         :rtype: nothing
#         """
#         self.stack1.pop()
#
#
#     def peek(self):
#         """
#         :rtype: int
#         """
#         return self.stack1[-1]
#
#     def empty(self):
#         """
#         :rtype: bool
#         """
#         return len(self.stack1) == 0

class Queue(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.stack1 = []
        self.stack2 = []


    def push(self, x):
        """
        :type x: int
        :rtype: nothing
        """
        self.stack1.append(x)

    def pop(self):
        """
        :rtype: nothing
        """
        if len(self.stack2) == 0:
            while len(self.stack1):
                curr = self.stack1.pop()
                self.stack2.append(curr)
        self.stack2.pop()


    def peek(self):
        """
        :rtype: int
        """
        if len(self.stack2) == 0:
            while len(self.stack1):
                curr = self.stack1.pop()
                self.stack2.append(curr)
        return self.stack2[-1]

    def empty(self):
        """
        :rtype: bool
        """
        return len(self.stack1) + len(self.stack2) == 0
Download Implement Queue using Stacks.py

List of all Implement Queue using Stacks problems

Leetcode 232 Implement Queue using Stacks problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

initialize your data structure here.

"""

self.stack1 = []

self.stack2 = []

def push(self, x):

"""

:type x: int

:rtype: nothing

"""

while len(self.stack1) > 0:

curr = self.stack1.pop()

self.stack2.append(curr)

self.stack1.append(x)

while len(self.stack2) > 0:

curr = self.stack2.pop()

self.stack1.append(curr)

def pop(self):

"""

:rtype: nothing

"""

self.stack1.pop()

def peek(self):

"""

:rtype: int

"""

return self.stack1[-1]

def empty(self):

"""

:rtype: bool

Feedback is the most important part of any website.

If you have any query, suggestion or feedback, Please feel free to contact us.