题目链接:从头到尾打印链表
import java.util.*;
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> result = new ArrayList<Integer>();
Stack<Integer> stack = new Stack<Integer>();
while(listNode != null)
{
stack.push(listNode.val);
listNode = listNode.next;
}
while(!stack.empty())
{
result.add(stack.pop());
}
return result;
}
}