题解 | #从尾到头打印链表#
从尾到头打印链表
http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
垫底效率,又要优化!!!
* public class ListNode {
* int val;
* ListNode next = null;
*
* ListNode(int val) {
* this.val = val;
* }
* }
*
*/
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ListNode list1 = listNode;
ArrayList<Integer> list2 = new ArrayList<Integer>();
while(list1!=null){
list2.add(list1.val);
list1 = list1.next;
}
int num3 = list2.size();
ArrayList<Integer> list3 = new ArrayList<Integer>();
for(int i = 0; i < num3; i++){
//int a = list2.get(num3-1-i);
list3.add(list2.get(num3-1-i));
}
//Collections.reverse(list2);
return list3;
}
}