数据结构与算法

两个栈实现一个队列

2019-12-19  本文已影响0人  而立之年的技术控
微信图片_20191219182351.jpg
class Solution:
    def __init__(self):
        self.stack1 = []
        self.stack2 = []
    def push(self, node):
        # write code here
        self.stack1.append(node)
    def pop(self):
        # return xx
        if self.stack1 == [] and self.stack2 == []:
            return None
        if self.stack2 == []:
            while self.stack1:
                self.stack2.append(self.stack1.pop())
        return self.stack2.pop()
上一篇 下一篇

猜你喜欢

热点阅读