package javaOffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class o03_ListFromTailtoHead_03 {
public class ListNode{
public int val;
public ListNode next=null;
public ListNode(int val) {
this.val = val;
}
}
//递归方式
ArrayList arrayList=new ArrayList();
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
if(listNode!=null){
printListFromTailToHead(listNode.next);
arrayList.add(listNode.val);
}
return arrayList;
}
//非递归模式
public ArrayList<Integer> printListFromTailToHeadStack(ListNode listNode){
Stack<Integer> stack=new Stack<>();
while(listNode!=null){
stack.push(listNode.val);
listNode=listNode.next;
}
ArrayList arrayList=new ArrayList();
while(stack!=null){
arrayList.add(stack.pop());
}
return arrayList;
}
}