标签:使用 ext off print highlight list array 逆置 ret
输入一个链表,按链表从尾到头的顺序返回一个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;
}
}
标签:使用 ext off print highlight list array 逆置 ret
原文地址:https://www.cnblogs.com/blzm742624643/p/12229842.html