C : have a ERROR. Need help !


  • 0
    W

    /**

    • Definition for singly-linked list.
    • typedef struct ListNode{
    • int val;
      
    • struct ListNode *next;
      
    • }ListNode;
      /
      /
      *
    • Return an array of size *returnSize.
    • Note: The returned array must be malloced, assume caller calls free().
      */

    struct ListNode** splitListToParts(struct ListNode* root, int k, int* returnSize) {
    struct ListNode* root_copy = root;
    int i=0;

    while(root_copy != NULL){
        i ++ ;
        root_copy  = root_copy->next;
    
    }
    int mod = i%k;
    int bei = i/k;
    int m,b,n;
     struct ListNode *ret ,*p;
    
     struct ListNode **ret_t ;
    
         ret_t = (struct ListNode **)malloc(sizeof(struct ListNode **)*i);
         for( m=0;m<mod;m++){
             p = (struct ListNode *)malloc(sizeof(struct ListNode *));
             ret = p;
             for( n=0;n<bei+1;n++){
                 ret->next = (struct ListNode *)malloc(sizeof(struct ListNode *));
                 ret = ret->next;
                 ret->val = root->val;
                 root = root->next;
             }
    
             *ret_t = p->next ;
             ret_t ++;
             free(ret);
             free(p);
         }
         for(b = mod+1; b<=k;b++){
              p = (struct ListNode *)malloc(sizeof(struct ListNode *));
             ret= p;
             for(n=0;n<bei;n++){
                 ret->next = (struct ListNode *)malloc(sizeof(struct ListNode *));
                 ret = ret->next;
                 ret->val = root->val;
                 root = root->next;
             }
             *ret_t = p->next ;
             ret_t ++;
             free(ret);
             free(p);
         }
    
    //*returnSize =
    return ret_t;
    

    }

    *ERROR:Line 34: store to address 0x000000b3d278 with insufficient space for an object of type 'struct ListNode '
    *Line 34 : ret->next = (struct ListNode *)malloc(sizeof(struct ListNode ));

    Can someone answer me why? very thanks


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.