AC,输入为空链表时的处理写了三次才写对,应返回长度为0的空数组。
第一次0ms,与题解不同思路
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
//if(head==null) return false;
//if(head==null) throw null;
if(head==null) return new int[0];
int length = 1;
ListNode p = head.next;
head.next = null;
while(p!=null){
ListNode p1 = p.next;
p.next = head;
head = p;
p = p1;
length++;
}
int[] arr = new int[length];
p = head;
for(int i = 0;i < length; i++){
arr[i] = p.val;
p = p.next;
}
return arr;
}
}