输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
牛客上不能用stack ,一直不给我ac,只好又list接受了,都一样,但是堆栈更有代入感一点
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> result = new ArrayList<Integer>();
ListNode temp = listNode;
while ( temp != null ) {
list.add( temp.val );
temp = temp.next;
}
for ( int i = list.size()-1; i>=0; i-- ) {
result.add( list.get(i) );
}
return result;
}
}