简体   繁体   English

链接列表中的运行时错误:“ListNode”类型的 null 指针中的成员访问

[英]Runtime error in Linked List :member access within null pointer of type 'ListNode'

I am given two non-empty linked lists representing two non-negative integers and i am trying to Add the two numbers and return the sum as a linked list.我得到两个代表两个非负整数的非空链表,我试图将这两个数字相加并将总和作为链表返回。 And i am getting an error which says我收到一个错误,上面写着

Char 18: runtime error: member access within null pointer of type 'ListNode' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior字符 18:运行时错误:null 类型“ListNode”(solution.cpp)指针内的成员访问摘要:UndefinedBehaviorSanitizer:未定义行为

Why am i getting this array and How does i solve this?为什么我得到这个数组,我该如何解决这个问题?

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int carry=0,sum = 0;
        ListNode* res = new ListNode(0);
        while(l1->next !=NULL){
            sum = carry + l1->val + l2->val;
            if(sum>9)
                carry = 1;
            else
                carry = 0;
            
            res = res->next;
            
            res->val = sum;
        }
        return res;
        
    }
};

The error is showing in Line with code错误显示在与代码一致

         res->val = sum;

This code has many bugs first of all, you are not updating the value of l2 so ultimately you are trying to add all the values of l1 with the head->node of l2 which is absurd.这段代码首先有很多错误,你没有更新 l2 的值,所以最终你试图将 l1 的所有值与 l2 的 head->node 相加,这是荒谬的。

As for the error, its happening because you are creating a new node ListNode* res = new ListNode(0);至于错误,它的发生是因为您正在创建一个新节点ListNode* res = new ListNode(0); So according to this a new node is created with name res and res->next will contain NULL , this I am guessing as per the constructor call for your ListNode class.因此,根据此创建一个名为 res 的新节点, res- res->next将包含NULL ,我猜测这是根据您的ListNode class 的构造函数调用。

So w.r.t above statement res = res->next;所以 w.r.t 以上语句res = res->next; Now res is pointing to NULL.现在 res 指向 NULL。 Further res->val = sum;进一步res->val = sum; you are trying to access the value inside null thus giving you runtime error because you are trying to access something which is not assigned.您正在尝试访问 null 中的值,从而给您运行时错误,因为您正在尝试访问未分配的内容。

I would recommend you to first try simple problem ' Adding 1 to a given linked list '.我建议您首先尝试简单的问题“将 1 添加到给定的链表”。 As it can also generate carry when you add 1 to 999 and resulting linked list should be 1000.因为当你将 1 添加到 999 时它也可以生成进位,结果链表应该是 1000。
It will give you a better understanding on how to tackle this type of problems.它将使您更好地了解如何解决此类问题。

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

相关问题 运行时错误:null 类型“ListNode”指针内的成员访问 - 克隆链表 - runtime error: member access within null pointer of type 'ListNode' - Clone a linked list 运行时错误:“ListNode”类型的 null 指针内的成员访问 - runtime error: member access within null pointer of type 'ListNode' 'struct ListNode' 类型的空指针内的成员访问 - 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