题目描述
输入一个链表,从尾到头打印链表每个节点的值
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
import java.util.*;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> s = new Stack<Integer>();
ArrayList<Integer> r = new ArrayList<Integer>();
while(listNode!=null){
s.push(listNode.val);
listNode = listNode.next;
}
while(!s.empty()){
r.add(s.pop());
}
return r;
}
}