//cur指当前节点
//nex用来保存当前节点的下一个节点
//pre用来指向已经反转节点的头节点。
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
//返回的是一个数组
//首先定义一个数组
ArrayList<Integer> list=new ArrayList<>();
//定义三个
//cur指向当前节点
//nex指向当前节点的下一个节点
//pre指向已经反转节点的尾部节点
if(listNode==null)
{
return list;
}
//初始化
ListNode cur=listNode;
ListNode nex=null;
ListNode pre=null;
while(cur!=null)
{//先保存当前节点的下一个节点
nex=cur.next;
// 将第一个指针指向pre;
//1.当前节点为1时,pre=null,令1指向nul
//2.当前节点为2时,pre=1,令2指向1
//依次次类推,实现3->2->1->null
//返回数组,由于pre指向的已经反转链表的头节点,则pre不为空的时候依次将节点加入数组
cur.next=pre;
pre=cur;
cur=nex;
}
while(pre!=null)
{
list.add(pre.val);
pre=pre.next;
}
return list;
}
}