错误题目:删除排序链表中的重复元素

例如 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‘相关推荐

  1. 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 ...

  2. LeetCode Line x: Char x: runtime error: member access within null pointer of type ‘struct ListNode

    今天做leetcode的时候使用链表遇到了这个问题,百思不得其解.遂疯狂百度标题,发现大伙都说了一个问题,那就是没有对指针判空.没办法,鄙人能力有限不知道为什么这个环境下不判空会报错,不过既然知道原因 ...

  3. LeetCode报错:runtime error: member access within null pointer of type ‘struct ListNode‘

    错误题目:876. 链表的中间结点 错误原因:试图使用空指针 解决方法:找出等价判断条件进行替换,排除对空指针的引用. /*** Definition for singly-linked list.* ...

  4. :runtime error: member access within null pointer of type ‘struct ListNode‘报错

    该问题为刷力扣时,常见报错. 错误原因:通常是之前为struct ListNode分配了内存,但是其中指针未分配地址,导致系统认为其为野指针. 解决方案:如果为空,就令其指向NULL 如果不为空就加入 ...

  5. LeetCode报错: “runtime error: member access within null pointer of type ‘struct ListNode”

    LeetCode里面会经常使用到ListNode这个数据结构,这个报错原因是对空指针进行了引用,但是在测试的时候并不会出现报错,只有在提交的时候才会出现报错. 两者出现区别的原因在于:在测试中给出的测 ...

  6. 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 ...

  7. 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 ...

  8. 【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 ...

  9. leetcode报错:member access within null pointer of type struct ListNode

    leetcode报错:member access within null pointer of type 'struct ListNode'

最新文章

  1. rate-limit
  2. 《C#3.0 in a Nutshell ,3rd Edition》之序言篇
  3. 链接器怎样使用静态库来解决符号引用
  4. Openfiler的安装和配置
  5. openwrt 更改 debug 等级(hostapd)
  6. c语言数字字体的格式,c语言—— 格式控制符—— 数据类型——相对应的字节数...
  7. .netframework3.5 中TimeZoneInfo 类的使用
  8. 基于json-lib.jar包Json实例程序
  9. 安卓应用安全指南 4.6.3 处理文件 高级话题
  10. ssh无密码登录_3个简单步骤即可完成无密码SSH登录
  11. 全奖热招 | TUM、HKU、McGill、UTS等8所高校全奖博士招生信息汇总
  12. 【程序员金典】字符串互异
  13. [work] 一阶 二阶马尔可夫
  14. Windows获取模块基地址
  15. 【论文学习】《Who is Real Bob? Adversarial Attacks on Speaker Recognition Systems》
  16. 孟岩老师:Linux之父话糙理不糙
  17. 光大证券自称因异常交易损失约1.94亿元,疑为程序问题!
  18. 狂神说——大前端进阶NodeJS、Npm、Es6、Babel、Webpack、模块化使用
  19. Jython安装小坎坷
  20. 应广单片机红外发射例程

热门文章

  1. 使用phpspreadsheet导出数据时内存溢出处理
  2. 规则引擎Drools使用 第四篇Drools基础语法
  3. 电脑热键冲突怎么修改?Windows11热键设置更改方法
  4. 画出千军万马!远古神兵听凭你的「指」挥
  5. matlab矩阵最大无关组,MATLAB 第十二章 矩阵的秩与向量组的最大无关组.ppt
  6. 终于等来《仙剑奇侠传四》,全新阵容太可了!
  7. 安卓扁平化之路专题(一)Android 4.4新特性
  8. 面经+经验分享|2019秋招算法岗复盘
  9. 九、wxWidgets菜单
  10. 在VUE中使用RSA加密解密加签