首页 > 其他 > 详细

从尾到头打印链表

时间:2019-11-08 00:30:33      阅读:93      评论:0      收藏:0      [点我收藏+]

输入一个链表,按链表从尾到头的顺序返回一个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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!