简体   繁体   English

运行时错误:null 类型“ListNode”指针内的成员访问 - 克隆链表

[英]runtime error: member access within null pointer of type 'ListNode' - Clone a linked list

I am trying to clone a linked list in reverse order.我正在尝试以相反的顺序克隆链表。

    ListNode* cloneList(ListNode* head) {
        ListNode *prev = new ListNode(head->val);
        head = head->next;
        while (head != NULL)
        {
            ListNode *p = new ListNode(head->val, prev);
            head = head->next;
            prev = p;
        }
        return prev;
    }

The definition of ListNode is as follows: ListNode的定义如下:

struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

But i am getting this runtime error: member access within null pointer of type 'ListNode'.但我收到此运行时错误:成员访问类型为“ListNode”的 null 指针。

Am i making some mistake in creating or initialising the node?我在创建或初始化节点时犯了一些错误吗? Please explain.请解释。

It is unusual for a clone method to return a new list in reverse order, but OK, if that is your actual requirement.克隆方法以相反的顺序返回新列表是不常见的,但是如果这是您的实际要求,那没关系。

As stated in comments, the code you have shown does not account for the possibility of the list being empty (ie, when head is nullptr ), eg:如评论中所述,您显示的代码不考虑列表为空的可能性(即,当headnullptr时),例如:

ListNode* cloneList(ListNode* head) {
    if (!head) return nullptr; // <-- add this
    ...
}

If head is not nullptr and points to a valid ListNode , and the list is properly null-terminated, then your code works just fine.如果head不是nullptr并且指向一个有效的ListNode ,并且该列表正确地以 null 终止,那么您的代码就可以正常工作。

Online Demo在线演示

However, it can be simplified a bit, eg:但是,它可以简化一点,例如:

ListNode* cloneList(ListNode* head) {
    ListNode *newHead = nullptr;
    while (head) {
        newHead = new ListNode(head->val, newHead);
        head = head->next;
    }
    return newHead; 
}

Online Demo在线演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 链接列表中的运行时错误:“ListNode”类型的 null 指针中的成员访问 - Runtime error in Linked List :member access within null pointer of type 'ListNode' 运行时错误:“ListNode”类型的 null 指针内的成员访问 - runtime error: member access within null pointer of type 'ListNode' &#39;struct ListNode&#39; 类型的空指针内的成员访问 - member access within null pointer of type 'struct ListNode' 螺旋矩阵挑战:“ListNode”类型的 null 指针内的成员访问 - Spiral matrix challenge: member access within null pointer of type 'ListNode' 为什么我在“ListNode”类型的 null 指针中获得此错误成员访问权限 - Why am I getting this error member access within null pointer of type 'ListNode' 为什么我在 null 指针错误(链表)中获得成员访问权限 - why do I get member access within null pointer error (linked list) 为什么我会收到此运行时错误:null 类型“Solution::node”(solution.cpp) 指针中的成员访问 - Why am I getting this runtime error: member access within null pointer of type 'Solution::node' (solution.cpp) C++,运行时错误:成员调用 null 类型的指针 - C++,runtime error: member call on null pointer of type 使用struct作为私有成员在链接列表类中定义ListNode - Using struct as private member to define ListNode within link list class 链表空指针错误 C++ - linked list null pointer error c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM