leetcode-剑指06-OK

address

/**
 * 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){
    int getlen(struct ListNode* head){
        int len=0;
        while(head != NULL){
            len++;
            head = head->next;
        }
        return len;
    }
    int getval(struct ListNode* head){
        return head->val;
    }
    int len = getlen(head);
    returnSize[0] = len;
    if(len==0)
        return NULL;
    int *answer = (int *)malloc(sizeof(int) * len);
    for(int i = len-1; i>= 0; i--){
        answer[i] = getval(head);
        head = head->next;
    }
    return answer;
}
posted @ 2021-01-21 20:18  RougeBW  阅读(36)  评论(0编辑  收藏  举报