当前位置:   article > 正文

leetcode链表相关的问题_tail = malloc(sizeof(struct listnode));

tail = malloc(sizeof(struct listnode));

总结一下在做leetcode第二题两数之和时遇到的问题。

问题一:

在leetcode中如何实现尾插,在尾插定义新节点时总会报如下错误:

runtime error: member access within null pointer of type 'struct ListNode' [solution.c]

这是使用了空指针,其实是没有给指针申请空间。

  1. struct ListNode* tail=NULL;
  2. tail = malloc(sizeof(struct ListNode)); //如果没有此句会报错

问题二:

在解决问题一后运行代码依旧会报如下错误:

Line 70: Char 15: runtime error: member access within misaligned address 0xbebebebebebebebe for type 'struct ListNode', which requires 8 byte alignment [ListNode.c] 0xbebebebebebebebe: note: pointer points here <memory cannot be printed>

这是指针的*next没有赋值NULL,记得添加:

tail->next = NULL;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/945580
推荐阅读
相关标签
  

闽ICP备14008679号