一、介绍
利用栈这种数据结构,将各个节点压入栈中,利用先进后出特点,完成倒序打印。
二、代码
public void reversePrint(Node head) {
if (head.next == null) {
return;
}
Stack<Node> stack = new Stack<>();
Node temp = head.next;
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}