标签:
public class Solution {// 递归实现 ArrayList<Integer> result = new ArrayList<>(); public ArrayList<Integer> printListFromTailToHead(ListNode listNode){ if(listNode != null){ this.printListFromTailToHead(listNode.next); result.add(listNode.val); } return result; }
// 借用stack public ArrayList<Integer> printListFromTailToHead3(ListNode listNode) { Stack<Integer> stack = new Stack<>(); ArrayList<Integer> list = new ArrayList<>(); while(listNode!=null){ stack.push(listNode.val); listNode = listNode.next; } while(!stack.isEmpty()){ list.add(stack.pop()); } return list; } }
标签:
原文地址:http://www.cnblogs.com/hesier/p/5575512.html