ArrayList<Integer> list = new ArrayList<>();
if(head == null ){
return new int[0];
}
if(head.next == null){
return new int[]{head.val};
}
ListNode cur = head.next;
head.next = null;
while(cur != null){
ListNode curNext = cur.next;
cur.next = head;
head = cur;
cur = curNext;
}
cur = head;
while(cur != null){
list.add(cur.val);
cur = cur.next;
}
int[] ret = new int[list.size()];
for(int i = 0;i< ret.length;i++){
ret[i] = list.get(i);
}
return ret;