c语言数据结构从尾到头打印链表



/**
 * 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){
    struct ListNode* cur = head;
    struct ListNode* newNode = NULL;
    int j=0;
    while(cur)
    {
        ++j;
        struct ListNode* next = cur->next;
        cur->next = newNode;
        newNode = cur;
        cur = next;
    }
    *returnSize = j;
    int i=0;
    int *array = (int*)malloc(sizeof(int)*(j));
    while(cur)
    {
        array[i++] = cur->val;
        cur = cur->next;
    }
    return array;
}

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
其中,
*returnSize = j;这一句很有必要嘛,为什么要写这一句呢,没有了为什么报错呢

*returnSize = j; 是 返回 链表中的元素的个数吧,
你不设置,那么外面再使用 这个返回的 数组指针时,就没办法知道 有多少数据需要处理了