runtime error: member access within null pointer of type ‘struct ListNode‘
错误题目:删除排序链表中的重复元素
例如 1->1->2
输出 1->2
错误原因:试图使用空指针
解决方法:增加判断条件,并且判断的顺序不能改变。排除对空指针的引用。
/*** Definition for singly-linked list.* 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) {}* };*/
class Solution {public:ListNode* deleteDuplicates(ListNode* head) {/* if (head == nullptr || head->next == nullptr) {return head;}*/if (head->next == nullptr || head == nullptr) {return head;}ListNode* cur = head->next;ListNode* node = head;while (cur != nullptr) {if (cur->val == node->val) {node->next = cur->next;cur = cur->next;}else {cur = cur->next;node = node->next;}}return head;}
};
上面的代码看上去逻辑上是没有错误的,但是在判断空指针的时候,报错。
报错是这样一段话:
runtime error: member access within null pointer of type 'struct ListNode'
这句话的意思就是,试图使用空指针。在判断如下语句时候出现错误。
if (head->next == nullptr || head == nullptr)
我们可以看到,假如head
是空指针的话,那么head->next
本身是不合法,因此会报错,但是我们如果把顺序改一下就没错了
if (head == nullptr || head->next == nullptr)
。
这是做题时候的一个错误,希望可以记住。
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 ...
- LeetCode Line x: Char x: runtime error: member access within null pointer of type ‘struct ListNode
今天做leetcode的时候使用链表遇到了这个问题,百思不得其解.遂疯狂百度标题,发现大伙都说了一个问题,那就是没有对指针判空.没办法,鄙人能力有限不知道为什么这个环境下不判空会报错,不过既然知道原因 ...
- 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 如果不为空就加入 ...
- LeetCode报错: “runtime error: member access within null pointer of type ‘struct ListNode”
LeetCode里面会经常使用到ListNode这个数据结构,这个报错原因是对空指针进行了引用,但是在测试的时候并不会出现报错,只有在提交的时候才会出现报错. 两者出现区别的原因在于:在测试中给出的测 ...
- 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 ...
- 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 ...
- 【Leetcode记录】runtime error: member access within null pointer of type ‘ListNode‘ (solution.cpp) SUMMA
环形链表快慢指针: runtime error: member access within null pointer of type 'ListNode' (solution.cpp) SUMMARY ...
- leetcode报错:member access within null pointer of type struct ListNode
leetcode报错:member access within null pointer of type 'struct ListNode'
最新文章
- rate-limit
- 《C#3.0 in a Nutshell ,3rd Edition》之序言篇
- 链接器怎样使用静态库来解决符号引用
- Openfiler的安装和配置
- openwrt 更改 debug 等级(hostapd)
- c语言数字字体的格式,c语言—— 格式控制符—— 数据类型——相对应的字节数...
- .netframework3.5 中TimeZoneInfo 类的使用
- 基于json-lib.jar包Json实例程序
- 安卓应用安全指南 4.6.3 处理文件 高级话题
- ssh无密码登录_3个简单步骤即可完成无密码SSH登录
- 全奖热招 | TUM、HKU、McGill、UTS等8所高校全奖博士招生信息汇总
- 【程序员金典】字符串互异
- [work] 一阶 二阶马尔可夫
- Windows获取模块基地址
- 【论文学习】《Who is Real Bob? Adversarial Attacks on Speaker Recognition Systems》
- 孟岩老师:Linux之父话糙理不糙
- 光大证券自称因异常交易损失约1.94亿元,疑为程序问题!
- 狂神说——大前端进阶NodeJS、Npm、Es6、Babel、Webpack、模块化使用
- Jython安装小坎坷
- 应广单片机红外发射例程