输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000 (说明不能转成数字了,10000个位,太大了)
int* reversePrint(struct ListNode* head, int* returnSize)
{
//指着头 一会备用
struct ListNode* p=head;
int count=0;//节点数决定数组大小
for(;head!=NULL;head=head->next)
{
count++;
}
* returnSize=count;
int *ans=(int*)malloc(count*sizeof(int));//动态创建数组
assert(ans!=NULL);
count-=1;//下标从0开始 有效位数从1开始 -1使其一致
while(p!=NULL)
{
ans[count--]=p->val;//遍历一个赋一个值
p=p->next;
}
return ans;
}