题解 | #从尾到头打印链表#
从尾到头打印链表
https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
import java.util.ArrayList; public class Solution { //递归函数 public void recursion(ListNode head, ArrayList<Integer> res){ if(head != null){ //先往链表深处遍历 recursion(head.next, res); //再填充到数组就是逆序 res.add(head.val); } } public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> res = new ArrayList<Integer>(); //递归函数解决 recursion(listNode, res); return res; } }