输入一个链表,从尾到头打印链表每个节点的值。
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<Integer>();
if(null == listNode)
return list;
else{
Stack<Integer> s = new Stack<Integer>();
ListNode nextNode = listNode;
do{
//list.add(nextNode.val);
s.push(nextNode.val);
nextNode = nextNode.next;
}while(nextNode != null);
while(!s.empty()){
list.add(s.peek());
s.pop();
}
return list;
}
}
}