• 002 从头到尾打印链表


    一:程序主题

    1.题目

      输入一个链表,从尾到头打印链表每个节点的值。

    2.思路

    /*

    * 输入一个链表的头结点,从尾到头打印出每个结点的值
    * 解决方案一:首先遍历链表的节点后打印,典型的“后进先出”,可以使用栈来实现这种顺序。
    * 解决方案二:栈的本质就是递归,直接使用递归的方式,打印一个节点的时候先打印它后面的节点,再打印该节点自身,实现反向打印
    * 解决方案三:遍历链表,把链表中的元素复制到ArrayList中,然后逆序打印ArrayList中的元素
    * 解决方案四:前三种解决方案本身属于在打印链表的时候不修改链表本身结构,
    * 在允许修改链表结构的情况下可以把链表中的节点指针反转过来,改变链表方向,然后重新遍历打印改变方向后的链表,这种方式就不写了

    */

    二:程序

    1.ListNode类

     1 package com.jianke.it;
     2 /**
     3  * 节点类
     4  * @author dell
     5  *
     6  */
     7 public class ListNode {
     8     int val;
     9     ListNode next = null;
    10     public ListNode() {}
    11     public ListNode(int val) {
    12         this.val = val;
    13     }
    14 }

    2.方案二

    递归

     1 package com.jianke.it;
     2 
     3 import java.util.ArrayList;
     4 
     5 public class PrintListFromTailToHead {
     6     
     7     /**
     8      * 返回node
     9      */
    10     static ArrayList<Integer> arrayList=new ArrayList<Integer>();
    11     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {    
    12         if(listNode!=null){
    13             printListFromTailToHead(listNode.next);
    14             arrayList.add(listNode.val);
    15         }
    16         return arrayList;
    17         
    18     }
    19     public static void main(String[] args) {
    20         ListNode node1=new ListNode(1);
    21         ListNode node2=new ListNode(2);
    22         ListNode node3=new ListNode(3);
    23         ListNode node4=new ListNode(4);
    24         ListNode node5=null;
    25         ListNode node6=new ListNode(1);
    26         ListNode node7=new ListNode();
    27         node1.next=node2;
    28         node2.next=node3;
    29         node3.next=node4;
    30         ArrayList list1=printListFromTailToHead(node1);
    31         printInfo(list1);
    32 //        ArrayList list2=printListFromTailToHead(node5);
    33 //        printInfo(list2);
    34 //        ArrayList list3=printListFromTailToHead(node6);
    35 //        printInfo(list3);
    36 //        ArrayList list4=printListFromTailToHead(node7);
    37 //        printInfo(list4);
    38         
    39         
    40     }
    41     public static void printInfo(ArrayList list) {
    42         for(int i=0;i<list.size();i++) {
    43             System.out.print(list.get(i)+" ");
    44         }
    45         System.out.println("");
    46     }
    47 
    48 }

    3.方案一

    使用栈

     1 package com.jianke.it;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Stack;
     5 
     6 public class PrintListFromTailToHead {
     7     
     8     /**
     9      * 返回node
    10      */
    11     static ArrayList<Integer> arrayList=new ArrayList<Integer>();
    12     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {    
    13         Stack<Integer> stack=new Stack<Integer>();
    14         while(listNode!=null){
    15             stack.push(listNode.val);
    16             listNode=listNode.next;
    17         }
    18         while(!stack.isEmpty()){
    19             arrayList.add(stack.pop());
    20         }
    21         return arrayList;
    22         
    23     }
    24     public static void main(String[] args) {
    25         ListNode node1=new ListNode(1);
    26         ListNode node2=new ListNode(2);
    27         ListNode node3=new ListNode(3);
    28         ListNode node4=new ListNode(4);
    29         ListNode node5=null;
    30         ListNode node6=new ListNode(1);
    31         ListNode node7=new ListNode();
    32         node1.next=node2;
    33         node2.next=node3;
    34         node3.next=node4;
    35         ArrayList list1=printListFromTailToHead(node1);
    36         printInfo(list1);
    37         
    38     }
    39     public static void printInfo(ArrayList list) {
    40         for(int i=0;i<list.size();i++) {
    41             System.out.print(list.get(i)+" ");
    42         }
    43         System.out.println("");
    44     }
    45 
    46 }

    4.方案三

    使用ArrayList逆序打印链表

     1 package com.jianke.it;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Stack;
     5 
     6 public class PrintListFromTailToHead {
     7     
     8     /**
     9      * 返回node
    10      */
    11     static ArrayList<Integer> arrayList=new ArrayList<Integer>();
    12     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {    
    13         ArrayList<Integer> arrayList2=new ArrayList<Integer>();
    14         while(listNode!=null){
    15             arrayList.add(listNode.val);
    16             listNode=listNode.next;
    17         }
    18         for(int i=arrayList.size()-1;i>=0;i--){
    19             arrayList2.add(arrayList.get(i));
    20         }
    21         return arrayList2;
    22         
    23     }
    24     public static void main(String[] args) {
    25         ListNode node1=new ListNode(1);
    26         ListNode node2=new ListNode(2);
    27         ListNode node3=new ListNode(3);
    28         ListNode node4=new ListNode(4);
    29         ListNode node5=null;
    30         ListNode node6=new ListNode(1);
    31         ListNode node7=new ListNode();
    32         node1.next=node2;
    33         node2.next=node3;
    34         node3.next=node4;
    35         ArrayList list1=printListFromTailToHead(node1);
    36         printInfo(list1);
    37         
    38     }
    39     public static void printInfo(ArrayList list) {
    40         for(int i=0;i<list.size();i++) {
    41             System.out.print(list.get(i)+" ");
    42         }
    43         System.out.println("");
    44     }
    45 
    46 }
    
    
  • 相关阅读:
    docker将jar打包镜像文件
    特性阻抗(转)
    关于三极管偏置电路的思考
    怎样理解阻抗匹配?(转)
    你要包火到几时呢
    Bluetooth Note
    今年过年没回家
    第二天(tomcat与web程序结构与Http协议与HttpUrlConnection)
    JavaIO操作(1)转换流
    canphp框架功能与特性介绍
  • 原文地址:https://www.cnblogs.com/juncaoit/p/9004989.html
Copyright © 2020-2023  润新知