一个获取链表的长度的方法
初始化数组长度
反转方法中进行逆向循环赋值
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution{
public int[] reversePrint(ListNode head) {
int i=getlength(head);
int[] a=new int[i];
for(int n=a.length-1;n>=0;n--){
a[n]=head.val;
head=head.next;
}
return a;
}
public int getlength(ListNode head ){
int length=0;
if(head==null){
return 0;
}
while(head!=null){
length++;
head=head.next;
}
return length;
}
}