剑指 Offer 06. 从尾到头打印链表

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 
10 /**
11  * Note: The returned array must be malloced, assume caller calls free().
12  */
13 int* reversePrint(struct ListNode* head, int* returnSize){
14     int count=0;
15     struct ListNode *p=head;
16     while(p)
17     {
18         count++;
19         p=p->next;
20     } 
21      *returnSize = count;
22     int *arrays=(int *)malloc(sizeof(int)*count);
23     count--;
24     while(head)
25     {
26         arrays[count]=head->val;
27         head = head->next;
28         count--;
29     }
30    
31     return   arrays;
32 }

 

posted @ 2021-06-17 13:57  传说中的旅行者  阅读(23)  评论(0编辑  收藏  举报