反向打印链表先将链表存储在栈中,然后将栈中的元素输出,就可以实现链表的反向打印了
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
public class PrintListReversing {
public static void main(String[] args) {
List<Integer> list = new LinkedList<Integer>();
for (int i=0; i<10; i++)
list.add(i);
printListReversing(list);
}
static void printListReversing(List<Integer> list) {
Stack<Integer> stack = new Stack<Integer>();
for (int i : list) {
stack.push(i);
}
int size = stack.size();
for (int i=0; i<size; i++)
System.out.println(stack.pop());
}
}