韦到头打印链表
输入为链表的表头
输出为需要打印的“新链表”的表头
/**
* 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> result=
new
ArrayList();
if
(listNode==
null
)
return
result;
Stack<ListNode> stack=
new
Stack();
stack.push(listNode);
while
(listNode.next!=
null
)
{
stack.push(listNode.next);
listNode=listNode.next;
}
while
(!stack.isEmpty())
{
ListNode node=stack.pop();
result.add(node.val);
}
return
result;
}
}