1.头插法,在原地遍历
//先利用两个pre和next两个指针将头节点为cur的链表反转, //在依次遍历链表存储到一个ArrayList并返回 import java.util.ArrayList; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode cur) { ArrayList<Integer> list=new ArrayList<Integer>(); ListNode pre=null; ListNode next=null; while(cur!=null){ next=cur.next; cur.next=pre; pre=cur; cur=next; } while(pre!=null){ list.add(pre.val); pre=pre.next; } return list; } }
原文:https://www.cnblogs.com/Roni-i/p/10363153.html