/** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */ import java.util.Stack; import java.util.ArrayList;
public class Solution { public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> list = new ArrayList<Integer>(); ListNode head = listNode; Stackstack = newStack(); while (head != null){ stack.push(head.val); head = head.next; } while (!stack.empty()){ list.add((Integer) stack.pop()); } returnlist; } }