逆序一个栈,不申请额外空间,只使用递归函数
原创
©著作权归作者所有:来自51CTO博客作者放下也不自在的原创作品,请联系作者获取转载授权,否则将追究法律责任
public class RevertStack {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
while (!stack.isEmpty()) {
System.out.println(stack.pop());
private static void reverse(Stack<Integer> stack) {
if (stack == null || stack.isEmpty()) {
Integer last = getAndRemoveLast(stack);
private static Integer getAndRemoveLast(Stack<Integer> stack) {
Integer result = stack.pop();
Integer last = getAndRemoveLast(stack);