剑指offer:从尾到头打印链表

时间:2020-01-22 22:56:55   收藏:0   阅读:87

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

 

遇到这种逆置的问题,一般使用栈的先进后出的特性

 

public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
	ArrayList<Integer> res = new ArrayList<>();
		Stack<Integer> temp = new Stack<>();
		while(listNode!=null){
			temp.add(listNode.val);
			listNode = listNode.next;
		}
		
		while(!temp.isEmpty()){
			res.add(temp.pop());
		}
		return res;
    }
}

  

原文:https://www.cnblogs.com/blzm742624643/p/12229842.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!