通过方式2:栈的方式逆序打印;
方式1:反转较复杂,破坏空间结构
//方法:利用栈这个数据结构将各个节点压如到栈中,利用栈的现金后出的特点,实现逆序打印
public static void reversePrint(HeroNode head){
if(head.next==null){
return;
}
//创建一个栈,将各个节点压入栈
Stack<HeroNode> stack = new Stack<>();
HeroNode cur = head.next;
while (cur!=null){
stack.push(cur);
cur = cur.next;
}
//将栈中的节点打印出来
while(stack.size()>0){
System.out.println(stack.pop());
}
}