1.常规做法
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* reversePrint(struct ListNode* head, int* returnSize){
if(head==NULL){
*returnSize=0;
return NULL;
}
int count=0;
struct ListNode* p=head;
while(p){
p=p->next;
count++;
}
*returnSize=count;
int *res=(int *)malloc(sizeof(int*)*count);
struct ListNode* q=head;
while(count && q){
res[count-1]=q->val;
q=q->next;
count--;
}
return res;
}
2.还有一种递归做法
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* reversePrint(struct ListNode* head, int* returnSize){
if(head==NULL){
*returnSize=0;
return (int *)malloc(sizeof(int*)*10000);
}
int* res=reversePrint(head->next,returnSize);
res[(*returnSize)++]=head->val;
return res;
}