Clean Python Implementation using Two Stacks


  • 4
    Y
    class Queue(object):
        def __init__(self):
            self.input = []
            self.output = []
            
        def push(self, x):
            self.input.append(x)
    
        def pop(self):
            self.peek()
            return self.output.pop()
            
        def peek(self):
            if(self.output == []):
                while(self.input != []):
                    self.output.append(self.input.pop())
            return self.output[-1]
            
        def empty(self):
            return self.input == [] and self.output == []

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.