dfs递归,类似二叉树后序遍历
class Solution {
public int[] printListReversingly(ListNode head) {
ArrayList<Integer> res = new ArrayList<>();
dfs(head, res);
return res.stream().mapToInt(Integer::valueOf).toArray();
}
public void dfs(ListNode head, ArrayList<Integer>res) {
if(head == null)
return;
dfs(head.next, res);
res.add(head.val);
}
}