题目:
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList
思路:
递归或者栈
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
ArrayList<Integer> arr = new ArrayList<Integer>();
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if (listNode != null) {
this.printListFromTailToHead(listNode.next);
arr.add(listNode.val);
}
return arr;
}
}