首页 > 其他 > 详细

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

时间:2017-09-07 09:51:44      阅读:327      评论:0      收藏:0      [点我收藏+]
/*
 * 思路:将链表中的值一个一个取出来,压入一个栈中,然后弹出,就是从后到前的打印了
 */
public class PrintLinked {

	public static void main(String[] args) {
		System.out.println(printListFromTailToHead(new ListNode(1)));
	}
	
    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    	Stack<Integer> stack = new Stack<Integer>();
    	while(listNode != null) {
    		stack.push(listNode.val);
    		listNode = listNode.next;
    	}
    	
    	ArrayList<Integer> arr = new ArrayList<>();
    	while(!stack.empty()) {
    		arr.add(stack.pop());
    	}
        return arr;
    }
}


本文出自 “12212886” 博客,请务必保留此出处http://12222886.blog.51cto.com/12212886/1963291

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

原文:http://12222886.blog.51cto.com/12212886/1963291

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