标签:链表
/*
* 思路:将链表中的值一个一个取出来,压入一个栈中,然后弹出,就是从后到前的打印了
*/
public class PrintLinked {
public static void main(String[] args) {
System.out.println(printListFromTailToHead(new ListNode(1)));
}
public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack = new Stack<Integer>();
while(listNode != null) {
stack.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> arr = new ArrayList<>();
while(!stack.empty()) {
arr.add(stack.pop());
}
return arr;
}
}本文出自 “12212886” 博客,请务必保留此出处http://12222886.blog.51cto.com/12212886/1963291
标签:链表
原文地址:http://12222886.blog.51cto.com/12212886/1963291