线性数据结构案例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());
        }
    }

猜你喜欢

转载自www.cnblogs.com/gary97/p/12289106.html
今日推荐