输入一个链表,从尾到头打印链表每个节点的值。
先用栈存储,然后在输出
/**
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> str = new ArrayList<Integer>();
if(listNode == null)
return str;
Stack<Integer> stack = new Stack<Integer>();
while(listNode!=null)
{
stack.push(listNode.val);
listNode = listNode.next;
}
while(!stack.isEmpty())
{
str.add(stack.peek());
stack.pop();
}
return str;
}
}