标签:
输入一个链表,从尾到头打印链表每个节点的值
输入为链表的表头
输出为需要打印的“新链表”的表头
import java.util.ArrayList;
import java.util.Stack;
public class Solution03 {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (listNode == null) {
return null;
}
Stack stack = new Stack();
while (listNode != null) {
stack.add(listNode.val);
listNode = listNode.next;
}
while (!stack.empty()) {
result.add((Integer)stack.pop());
}
return result;
}
}
class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5608389.html