线性数据结构案例3 —— 从尾到头打印单链表

一、介绍

 利用栈这种数据结构,将各个节点压入栈中,利用先进后出特点,完成倒序打印。

二、代码

 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());
        }
    }
posted @ 2020-02-09 22:18  荏苒白驹  阅读(132)  评论(0)    收藏  举报
levels of contents