LeetCode Line x: Char x: runtime error: member access within null pointer of type ‘struct ListNode
今天做leetcode的时候使用链表遇到了这个问题,百思不得其解。遂疯狂百度标题,发现大伙都说了一个问题,那就是没有对指针判空。没办法,鄙人能力有限不知道为什么这个环境下不判空会报错,不过既然知道原因了就开始修改。
原代码如下,错误我圈起来了,因为没有对head和temp判断是否为空就开始访问,所以报错。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head){
struct ListNode* ptr=head;
struct ListNode* temp=NULL;
struct ListNode* res=ptr;
while(ptr->next!=NULL)
{
temp=ptr->next;
while(temp->next!=NULL&&temp->val==ptr->val)
temp=temp->next;
if(temp->next==NULL)
break;
ptr->next=temp;
ptr=ptr->next;
}
if(temp->val==ptr->val)
ptr->next=NULL;
else
ptr->next=temp;
return res;
}
修改后如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head){
if(head==NULL)
return NULL;
struct ListNode* ptr=head;
struct ListNode* temp=NULL;
struct ListNode* res=ptr;
while(ptr->next!=NULL)
{
temp=ptr->next;
while(temp->next!=NULL&&temp->val==ptr->val)
temp=temp->next;
if(temp->next==NULL)
break;
ptr->next=temp;
ptr=ptr->next;
}
if(temp!=NULL&&temp->val==ptr->val)
ptr->next=NULL;
else
ptr->next=temp;
return res;
}
对即将要访问的指针进行判空后再操作就没问题啦.
鄙人菜鸟一枚,请忽视我的垃圾代码...
LeetCode Line x: Char x: runtime error: member access within null pointer of type ‘struct ListNode相关推荐
- runtime error: member access within null pointer of type ‘struct ListNode‘错误
LeetCode203错误分析: (3条消息) LeetCode:runtime error: member access within null pointer of type 'struct Li ...
- runtime error: member access within null pointer of type ‘struct ListNode‘
错误题目:删除排序链表中的重复元素 例如 1->1->2 输出 1->2 错误原因:试图使用空指针 解决方法:增加判断条件,并且判断的顺序不能改变.排除对空指针的引用. /*** D ...
- LeetCode报错: “runtime error: member access within null pointer of type ‘struct ListNode”
LeetCode里面会经常使用到ListNode这个数据结构,这个报错原因是对空指针进行了引用,但是在测试的时候并不会出现报错,只有在提交的时候才会出现报错. 两者出现区别的原因在于:在测试中给出的测 ...
- LeetCode报错:runtime error: member access within null pointer of type ‘struct ListNode‘
错误题目:876. 链表的中间结点 错误原因:试图使用空指针 解决方法:找出等价判断条件进行替换,排除对空指针的引用. /*** Definition for singly-linked list.* ...
- :runtime error: member access within null pointer of type ‘struct ListNode‘报错
该问题为刷力扣时,常见报错. 错误原因:通常是之前为struct ListNode分配了内存,但是其中指针未分配地址,导致系统认为其为野指针. 解决方案:如果为空,就令其指向NULL 如果不为空就加入 ...
- runtime error: member access within null pointer of type ‘MyLinkedList::ListNode‘ (solution.cpp)
ERROR情况: Line 40: Char 12: runtime error: member access within null pointer of type 'MyLinkedList::L ...
- 2020-10-26runtime error: member access within null pointer of type ‘struct ListNode‘ (solution.cpp)错
runtime error: member access within null pointer of type 'struct ListNode' (solution.cpp)错误 /*** Def ...
- LeetCode报错:Line 923: Char 9: runtime error: reference binding to null pointer of type ‘std::__cxx11:
LeetCode报错 报错原因: Line 923: Char 9: runtime error: reference binding to null pointer of type 'std::__ ...
- Line 923: Char 9: runtime error: reference binding to null pointer of type ‘int‘ (stl_vector.h)
Leetcode 报错 Line 923: Char 9: runtime error: reference binding to null pointer of type 'int' (stl_ve ...
最新文章
- 柳传志大声对iphone说:这是在中国!
- RabbitMq 详解
- mySql 注入攻击
- 多线程设置操作时间间隔_听说,有位Java程序员学完这59道多线程面试题之后,直接面上阿里...
- 捕获异常_Recover捕获异常
- hashmap clone_Java HashMap clone()方法与示例
- 下列有关html文件结构的说法错误的是,计算机教师招聘考试_网络部分选择题+答案(12页)-原创力文档...
- mysql source导入_读取MySQL数据库中的数据【Python数据分析百例连载】
- TCP Server 实现 RPN计算器(Python)
- 技术和技术管理人员评价标准
- 打造个人电脑安全终极防线
- Spark-SQL与hive整合【版本spark1.6.0+hive0.14】--Standalone模式
- Ubuntu傻瓜式录制GIF图—— Peek
- git 常见用法及遇到的问题
- MME中DNS服务器的作用,2.1 EPC中通过DNS解析PGW IP地址实例
- Linux系统安装与使用基础之第二篇熟悉Linux操作系统
- 7、树莓派音频——jackd进阶篇
- 恶魔猎手(Demon Hunter)尤迪安(Illidan)的点点滴滴
- 4.1 拦截电话号码
- 软件 2.0 时代的程序分析
热门文章
- 显示器驱动程序 NVIDIA Windows Kernel Mode Driver Version 已停止响应 并且己成功恢复 解决方法...
- 点云粗配准算法-4pcs
- 官宣了!工坊精品公开课学习网址
- Mysql详细学习指南(超详细)
- 提升解决问题的能力:学会提问,就解决了一半
- 搭建基于qemu + eclipse的kernel调试环境(by quqi99)
- CTF-misc(功夫再高也怕菜刀)
- mtk驱动camera的调试总结1--原理
- win7 64位系统运行regsvr32.exe注册32位模块提示版本不兼容的解决方法
- 当男人爱上女人——学会诱导需求