ZHAOXIN_UESTC 2019-07-22 14:41 采纳率: 100%
浏览 3401

runtime error: member access within null pointer of type 'struct ListNode' ,该怎么改?求帮助!

leetcode 第二题Add Two Numbers,我的回答出现runtime error: member access within null pointer of type 'struct ListNode' ,该怎么改?求帮助!

问题:把两个链表各位相加,得到一个新链表
Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

 class Solution {
public:
    //把链表转化成整数
    int ConverseToInt(ListNode* l){
            int num=0,mul=1;
            while(l!=NULL){
                num=num+mul*l->val;
                l=l->next;
                mul*=10;
            }
            return num;
        }
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {

        int a=ConverseToInt(l1);
        int b=ConverseToInt(l2);

        int c=a+b;
        ListNode *newNumber=NULL;
        newNumber->val=c%10;
        c/=10;
        ListNode* returnNumber=NULL;
        returnNumber=newNumber;
        newNumber=newNumber->next;
        while(c!=0){
            newNumber->val=c%10;
            c/=10;
            newNumber=newNumber->next;
        }
        return returnNumber;
    }
};  
  • 写回答

1条回答 默认 最新

  • D_DreKing 2019-07-22 14:47
    关注

    ListNode *newNumber=NULL;

    newNumber->val=c%10;

    这句代码错了,你的newNumber是空指针。

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型