import java.util.*; /** * 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> ret = new ArrayList<>(); if(listNode==null) { return ret; } //插法 ListNode cur = listNode; ListNode pre = null; //新头节点 ListNode tmp = cur; while(cur!=null){ //记录cur下一个位置 tmp = cur.next; cur.next = pre; //头插 //改变心链表头节点 pre = cur; cur = tmp; //回到原链表继续头插 } while(pre!=null){ ret.add(pre.val); pre = pre.next; } return ret; } }