思路1:可以将链表进行反转,然后进行数据的输出即可,单链表反转地址如下https://blog.csdn.net/Kevinnsm/article/details/113763272

这个思路1肯定有很大的问题,反转之后会改变链表的结构,而且如果链表节点数过多,循环反转浪费资源;

思路2:毫无疑问肯定使用栈

核心代码如下

 public static void stackPrint(Node head) {
        if (head.next == null) {
            System.out.println("LinkedList is empty!");
            return;
        }
        Stack<Node> stack = new Stack<>();
        Node cur = head.next;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (stack.size() > 0) {
            System.out.println(stack.pop());
        }
    }
posted on 2021-02-08 22:34  凸凸大军的一员  阅读(124)  评论(0编辑  收藏  举报