题目:输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

  • 核心代码如下
 1  public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
 2         ArrayList<Integer> list = new ArrayList<Integer>();
 3         ArrayList<Integer> result = new ArrayList<Integer>();
 4         if(listNode == null){ //如果没有节点,无需反转,直接返回空的ArrayList集合即可
 5             return result;
 6         }
 7         if(listNode.next == null){ //如果链表中只有一个节点,无需反转,返回这个节点对应的ArrayList即可
 8            result.add(listNode.val);
 9             return result;
10         }
11         
12         //定义一个辅助指针,遍历链表
13         ListNode temp = listNode; 
14         while(temp != null){
15             list.add(temp.val);
16             temp = temp.next;
17         }
18         for(int i=list.size()-1; i>=0;i--){
19             result.add(list.get(i));
20         }
21         return result;
22     }
  • 完整代码+测试截图
 1 import java.util.ArrayList;
 2 
 3 public class Demo1 {
 4     public static void main(String[] args) {
 5         // 先创建多个节点,供测试使用
 6         ListNode listNode1 = new ListNode(1);
 7         ListNode listNode2 = new ListNode(2);
 8         ListNode listNode3 = new ListNode(3);
 9         ListNode listNode4 = new ListNode(4);
10         ListNode listNode5 = new ListNode(5);
11         // 把各个节点链起来
12         listNode1.next = listNode2;
13         listNode2.next = listNode3;
14         listNode3.next = listNode4;
15         listNode4.next = listNode5;
16         listNode5.next = null;
17         
18          System.out.print("原始链表中的数据:");
19          printList(listNode1);
20          
21          System.out.print("\n结果数据:");
22         //把链表的值逆序放到一个Arraylist集合中
23          ArrayList<Integer> printListFromTailToHead = printListFromTailToHead(listNode1);
24          System.out.print(printListFromTailToHead.toString());
25     }
26 
27 
28     /**
29      * 遍历单链表
30      * @param listNode
31      */
32     public static void printList(ListNode listNode) {
33         ListNode tempNode = listNode;
34         while(tempNode != null){
35             System.out.printf("%d\t",tempNode.val);
36             tempNode = tempNode.next;
37         }
38     }
39 
40 
41     /**
42      * 把单链表中的值存储到一个ArrayList集合中,并返回这个集合
43      * @param listNode
44      * @return
45      */
46 
47     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
48         ArrayList<Integer> list = new ArrayList<Integer>();
49         ArrayList<Integer> result = new ArrayList<Integer>();
50         if (listNode == null) { // 如果没有节点,无需反转,直接返回null即可
51             return result;
52         }
53         if (listNode.next == null) { // 如果链表中只有一个节点,无需反转,返回这个节点对应的ArrayList即可
54             list.add(listNode.val);
55             return list;
56         }
57         // 定义一个辅助指针,遍历链表
58         ListNode temp = listNode;
59         while (temp != null) {
60             //每遍历到一个节点,就把这个节点的数据存放到list集合中
61             list.add(temp.val);
62             temp = temp.next;
63         }
64         //把list集合中的数据,逆序存放到result集合中
65         for (int i = list.size() - 1; i >= 0; i--) {
66             result.add(list.get(i));
67         }
68         return result;
69     }
70 }
71 // 节点类(就是链表中一个一个的节点对象)
72 class ListNode {
73     int val;
74     ListNode next = null;
75 
76     public ListNode(int val) {
77         this.val = val;
78     }
79 }
  • 运行结果

   2-从尾到头打印链表 算法

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄