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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
public ArrayList<Integer> (ListNode listNode) {
ArrayList<Integer> list = new ArrayList<Integer>(); ListNode pNode = listNode;
if (pNode != null) { if (listNode.next != null) { list.addAll(printListFromTailToHead(listNode.next)); } list.add(listNode.val); }
return list; }
public ArrayList<Integer> printListFromTailToHead2(ListNode listNode) { ArrayList<Integer> revLists = new ArrayList<Integer>(); Stack<Integer> stack = new Stack<Integer>();
while (listNode != null) { stack.push(listNode.val); listNode = listNode.next; }
while (!stack.empty()) { revLists.add(stack.pop()); }
return revLists; }
public ArrayList<Integer> printListFromTailToHead1(ListNode listNode) { ArrayList<Integer> lists = new ArrayList<Integer>(); ArrayList<Integer> revLists = new ArrayList<Integer>();
while (listNode != null) { lists.add(listNode.val); listNode = listNode.next; }
for (int i = lists.size() - 1; i >= 0; i--) { System.out.println(lists.get(i)); revLists.add(lists.get(i)); }
return revLists; }
|
近期评论