解法:
push()操作:插入到stack1中
pop()操作:
stack2中有数字,就弹出stack2中的栈顶元素
stack2为空,就先把stack1中的所有数字移到stack2中,然后再弹出stack2中的栈顶元素。

c++

class Solution
{
public:
    void push(int node) {
        stack1.push(node);
    }

    int pop() {
        if(stack2.empty())
        {
            while(!stack1.empty())
            {
                int x = stack1.top();
                stack1.pop();
                stack2.push(x);
            }
        }
        int x = stack2.top();
        stack2.pop();
        return x;
    }
private:
    stack<int> stack1;
    stack<int> stack2;
};

java

import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    public void push(int node) {
        stack1.push(node);
    }
    public int pop() {
        if(stack2.isEmpty()) {
            while(!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}

python

class Solution:
    def __init__(self):
        self.stack1 = []
        self.stack2 = []
    def push(self, node):
        self.stack1.append(node);
    def pop(self):
        if not self.stack2:
            while self.stack1:
                self.stack2.append(self.stack1.pop())
        return self.stack2.pop()