美文网首页
单链表逆序打印

单链表逆序打印

作者: 1ff5a98e5398 | 来源:发表于2019-01-16 23:25 被阅读11次

    思路

    解题思路有多种:

    1.实现单链表逆转,然后输出
    2.利用栈
    3.递归
    等等

    递归方法

    这里主要使用递归方法(应该也是最优方法了吧),递归实现起来还是比较简单的,下面就用Java来实现递归逆序输出单链表

    结构定义

        public static class Node {
    
            int data;
    
            Node next;
    
        }
    
        public static class LinkList {
    
            Node head;
        }
    
    

    实现

       public static void reverPrint(Node node) {
            if (node == null) {
                return;
            }
            if (node.next != null) {
                reverPrint(node.next);
            }
            System.out.println(node.data);
       }
    

    相关文章

      网友评论

          本文标题:单链表逆序打印

          本文链接:https://www.haomeiwen.com/subject/xvzvdqtx.html