1. 程式人生 > >用兩個棧實現佇列python

用兩個棧實現佇列python

# -*- coding:utf-8 -*-
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 not len(self.stack1):
            return None
        while len(self.stack1):
            self.stack2.append(self.stack1.pop())
        xx = self.stack2.pop()
        while len(self.stack2):
            self.stack1.append(self.stack2.pop())
        return xx