一个栈依次压入1、2、3、4、5,那么从栈顶到栈底分别为5、4、3、2、1。将这个栈转置后,从栈顶到栈底围1、2、3、4、5,也就是实现栈中元素的逆序,但是只能用递归函数实现,不能用其他数据结构。
import java.util.Stack;
public class Main {
public static void reverse(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}
int i = getAndRemoveLastElement(stack);
reverse(stack);
stack.push(i);
}
public static int getAndRemoveLastElement(Stack<Integer> stack) {
int result = stack.pop();
if (stack.isEmpty()) {
return result;
} else {
int last = getAndRemoveLastElement(stack);
//在得到 last 后,会从当前的result逐步递归回第一个result
//需要执行完完整的方法,所以不是只压入一个result,压入的result的个数是stack中元素的个数减1
stack.push(result);
return last;
}
}
public static void main(String[] args) {
Stack<Integer> test = new Stack<Integer>();
test.push(1);
test.push(2);
test.push(3);
test.push(4);
test.push(5);
reverse(test);
while (!test.isEmpty()) {
System.out.println(test.pop());
}
}
}
转自------程序员代码面试指南IT名企算法与数据结构题目最优解