用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
1 # -*- coding:utf-8 -*- 2 class Solution: 3 def __init__(self): 4 self.list1 = [] 5 self.list2 = [] 6 7 def push(self, node): 8 self.list1.append(node) 9 # write code here 10 def pop(self): 11 if len(self.list2) == 0: 12 while len(self.list1) > 1: 13 top = self.list1.pop(-1) 14 self.list2.append(top) 15 return self.list1.pop(-1) 16 return self.list2.pop(-1) 17 # return xx