
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
/** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.ArrayList; import java.util.Stack; public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { Stack<ListNode> stack = new Stack(); while(null != listNode){ stack.push(listNode); listNode = listNode.next; } ArrayList<Integer> array = new ArrayList(stack.size()); while(!stack.isEmpty()){ array.add(Integer.valueOf(stack.pop().val)); } return array; } }
|
近期评论