首页 > 其他 > 详细

从尾到头打印链表

时间:2015-11-03 11:57:58      阅读:139      评论:0      收藏:0      [点我收藏+]

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

  

public class ListNode {
    public int data;
    public ListNode next = null;
    public ListNode(int data){
        this.data = data;
    
    }

 

public class searchLink {
    public ArrayList<Integer>printListFromTailToHead(ListNode listNode){
        if(listNode == null){
            ArrayList list = new ArrayList();
            return list;
        }
        Stack<Integer> stk = new Stack<Integer>();
        while(listNode != null){
            stk.push(listNode.data);
            listNode = listNode.next;
            }
        ArrayList<Integer> arr = new ArrayList<Integer>();
        while(!stk.isEmpty()){
            arr.add(stk.pop());
        }
        return arr;
    }
}

思路在于,借助栈的特点,先进后出。所以先压栈,堆栈,最后再弹栈到集合中就OK。

从尾到头打印链表

原文:http://www.cnblogs.com/yangsy0915/p/4932388.html

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