0.递归写法
public class Solution { ArrayList list = new ArrayList(); public ArrayList printListFromTailToHead(ListNode listNode) { if(listNode!=null){ printListFromTailToHead(listNode.next); list.add(listNode.val); } return list; } }
1.栈的简单运用
public ArrayList printListFromTailToHead(ListNode listNode) { ArrayList list = new ArrayList(); Stack stack = new Stack(); while (listNode != null) { stack.push(listNode.val); listNode = listNode.next; } while (!stack.empty()) { list.add(stack.pop()); } return list; }
2.调了一个函数
public class Solution { public ArrayList printListFromTailToHead(ListNode listNode) { ArrayList list = new ArrayList(); while (listNode != null) { list.add(0, listNode.val); listNode = listNode.next; } return list; } }