标签:line ram san cal pos empty public return null
输入一个链表,按链表从尾到头的顺序返回一个ArrayList。
代码实现
package 剑指offer;
import java.util.ArrayList;
import java.util.Stack;
/**
* @author WangXiaoeZhe
* @Date: Created in 2019/11/22 14:57
* @description:
*/
public class Main3 {
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
public static void main(String[] args) {
}
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> list = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
while (listNode != null) {
stack.push(listNode.val);
listNode = listNode.next;
}
while (!stack.empty()) {
list.add(stack.pop());
}
return list;
}
}
标签:line ram san cal pos empty public return null
原文地址:https://www.cnblogs.com/wuhen8866/p/11911629.html