从尾到头打印链表

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

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 
 4 namespace JianZhiOffer
 5 {
 6     public class ListNode
 7     {
 8         public int val;
 9         public ListNode next;
10         public ListNode(int x)
11         {
12             val = x;
13         }
14     }
15 
16     class ListFromTailToHead
17     {
18         public List<int> printListFromTailToHead(ListNode listNode)
19         {
20             // write code here
21             ListNode lNode = listNode;
22 
23             Stack<int> stackTemp = new Stack<int>();
24 
25             while (lNode != null)
26             {
27                 stackTemp.Push(lNode.val);
28                 lNode = lNode.next;
29             }
30 
31             List<int> list = new List<int>();
32 
33             foreach (int item in stackTemp)
34             {
35                 list.Add(item);
36             }
37 
38             return list;
39         }
40     }
41 }
原文地址:https://www.cnblogs.com/xiaolongren/p/11817111.html