runtime error: member access within null pointer of type 'struct ListNode' [solution.c]

runtime error: member access within null pointer of type 'struct ListNode' [solution.c]

我正在尝试使用非递归解决方案解决 leetcode 上的合并 k 排序列表问题。当我 运行 代码时,它会给出如下所述的错误。你能帮我解决这个问题吗? 错误在 lists[min_idx] = lists[min_idx]->next;

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* mergeKLists(struct ListNode** lists, int listsSize){
    
    struct ListNode *result;
    result = (struct ListNode *)malloc(sizeof(struct ListNode));
    result = NULL;
    struct ListNode *p;
    if (!listsSize){
        return result;
    }
    
    int min = INT_MAX;
    int min_idx = 0;
    int cnt=0;
    while (lists){
        for ( int i = 0 ; i<listsSize ; i++){
            if (lists[i]){
                cnt=0;
                if (lists[i]->val < min){
                    min = lists[i]->val;
                    min_idx = i;
                }
            }
            else {
                cnt++;
            }
        }
        if (cnt==listsSize){
            break;
        }
        
        struct ListNode *temp;
        temp = (struct ListNode *)malloc(sizeof(struct ListNode));
        temp->val = min;
        temp->next = NULL;
        if (result == NULL){
            result = temp;
            p = temp;
        }
        else {
            p->next = temp;
            p = p->next;
        }
        lists[min_idx] = lists[min_idx]->next;
    }
    return result;
}

错误:

Line 52: Char 40: runtime error: member access within null pointer of type 'struct ListNode' [solution.c]. 

对于初学者来说,即使是前两行也会产生内存泄漏

result = (struct ListNode *)malloc(sizeof(struct ListNode));
result = NULL;

这个while循环

while (lists){

可以是一个无限循环,因为按照惯例,this 指针不能等于 NULL,因为它是指向数组第一个元素的指针。如果用户特意将空指针传递给与函数要求相矛盾的函数,则可以等于NULL。

这段代码

    for ( int i = 0 ; i<listsSize ; i++){
        if (lists[i]){
            cnt=0;
            if (lists[i]->val < min){
                min = lists[i]->val;
                min_idx = i;
            }
        }
        else {
            cnt++;
        }
    }
    if (cnt==listsSize){
        break;
    }

不清楚,本质上没有意义。

直接的方法可以看下面的例子

struct ListNode * mergeKLists( struct ListNode **lists, size_t listsSize )
{
    struct ListNode *head = NULL;
    struct ListNode **current = &head;

    int empty = 1;

    do
    {
        size_t i = 0;

        while ( i < listsSize && lists[i] == NULL ) i++;

        empty = i == listsSize;

        if ( !empty )
        {
            size_t min = i;
            
            while ( ++i != listsSize )
            {
                if ( lists[i] != NULL && lists[i]->val < lists[min]->val )
                {
                    min = i;
                }
            }

            *current = lists[min];
            lists[min] = lists[min]->next;
            ( *current )->next = NULL;
            current = &( *current )->next;              
        }
    } while ( !empty );

    return head;
}
  • 成对组合
  • 分而治之
  • (原来的数组会被覆盖,结果在lists[0]

#include <stddef.h>

struct ListNode {
        struct ListNode *next;
        int val;
        };


struct ListNode *mergeTwolists(struct ListNode *one, struct ListNode *two)
{
struct ListNode *result, **pp;

if (!one) return two;
if (!two) return one;

result = NULL;
for (pp = &result; one && two; pp = &(*pp)->next) {
        if (one->val <= two->val) { *pp = one; one = one->next; }
        else { *pp = two; two = two->next; }
        }
*pp = (one) ? one : two;
return result;
}

struct ListNode *mergeKLists(struct ListNode **lists, unsigned nlist)
{
unsigned src,dst;

for(    ; nlist > 1;    ) {                     // while there are pairs
        for (src=dst=0; src < nlist ; src+=2) { // combine pairwise
                if (src+1 >= nlist) lists[dst++] = lists[src];
                else lists[dst++] = mergeTwolists(lists[src], lists[src+1]);
                }
        nlist = dst;    // recurse
        }
return lists[0];
}