天天看點

劍指offer 用兩個棧實作一個隊列 python實作

劍指offer 用兩個棧實作一個隊列 python實作

# -*- coding:utf-8 -*-

class Solution:

    def __init__(self):

        self.stack1 = []

        self.stack2 = []

    def push(self, node):

        self.stack1.append(node)

    def pop(self):

        if self.stack2:

            return self.stack2.pop()

        else:

            while self.stack1:

                self.stack2.append(self.stack1.pop())

            return self.stack2.pop()