可以翻转链表之后 遍历打印 但是破坏了链表原有的结构,不推荐。
使用栈 先进后出 将节点遍历压入栈中,出栈打印
public static void reversePrint(Node head){ if (head.next == null){ return;//空链表,不能打印 } Stack<Node> stack = new Stack(); Node cur = head.next; while (cur != null){ //入栈 stack.push(cur); cur = cur.next; } while (stack.size()>0){ //出栈 Node pop = stack.pop(); System.out.println(pop); } }
浙公网安备 33010602011771号