import java.util.ArrayList;
//从尾到头打印链表
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> arrayList = new ArrayList<>();
add(arrayList, listNode);
return arrayList;
}
public static void add(ArrayList<Integer> xArrayList, ListNode listNode) {
// TODO Auto-generated method stub
if (listNode != null) {
add(xArrayList, listNode.next);
//递归回溯
xArrayList.add(listNode.val);
}
}
}
【剑指offer】从尾到头打印链表
最新推荐文章于 2024-01-24 22:27:37 发布