[英]member access within null pointer of type 'struct ListNode'
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == NULL) return false;
ListNode* walker = head;
ListNode* runner = head;
while(runner->next != NULL && walker->next != NULL){
walker = walker->next;
runner = runner->next->next;
if(walker == runner) return true;
}
return false;
}
};
I was practicing an interview code which seemed to be pretty simple.我正在练习一个看起来很简单的面试代码。 I have to return a bool that determines whether or not the singly-linked list has a cycle.
我必须返回一个 bool 来确定单向链表是否有循环。 I made two pointers walker which moves 1 step and runner which moves 2 steps every iteration.
我制作了两个指针步行器,每次迭代移动 1 步,跑步者移动 2 步。
But then this code gave me an error:但是后来这段代码给了我一个错误:
Line 15: member access within null pointer of type 'struct ListNode'
What causes that error?是什么导致了这个错误?
You only make sure that runner->next
is not null, however after assignment你只需要确保
runner->next
不为空,但是在赋值之后
runner = runner->next->next;
runner
can become null. runner
可以变为 null。
This should solve it:这应该解决它:
bool hasCycle(ListNode *head) {
if(head == NULL || head->next == NULL) { return false; }
struct ListNode * walker = new ListNode(1);
struct ListNode * runner = new ListNode(2);
walker = head;
runner = walker->next;
while(walker != fast){
if(runner == NULL || runner->next == NULL) { return false; }
walker = walker->next;
runner = runner->next->next;
}
return true;
}
//- If a loop from circular path then there is hundred% chance that they will going to meet at some point so here we are taking a walker which move one step and a runner which move two step. //- 如果从圆形路径循环,那么他们有 100% 的机会会在某个时刻相遇,所以这里我们要走一个走一步的步行者和一个走两步的跑步者。
bool hasCycle(ListNode *head){
if(head == NULL || head->next == NULL)
return false;
struct ListNode *temp = head;
struct ListNode *walker;
struct ListNode *runner;
walker = runner= head;
while(temp ){
walker = walker->next;
runner = runner->next->next;
if(runner == walker) // as soon both get at same address we got return as
true value.
{ return True; }
temp = temp->next;
}
return false;
} }
The Below Code should work fine.下面的代码应该可以正常工作。
It is the classic Hare-Tortoise theorem, where the hare takes 2 steps(distance units) while the tortoise goes by 1.这是经典的Hare-Tortoise定理,其中兔子走 2 步(距离单位),而乌龟走 1 步。
I think you did not check the nullity of runner->next->next
which has caused this error我认为您没有检查导致此错误的
runner->next->next
的无效性
bool hasCycle(ListNode *head)
{
if(head == NULL || head->next == NULL)
{
return false;
}
ListNode* tortoise=new ListNode();
ListNode* hare = new ListNode();
tortoise=head;
hare=tortoise->next;
while(tortoise != hare)
{
if(hare == NULL || hare->next == NULL)
{
return false;
}
tortoise=tortoise->next;
hare=hare->next->next;
}
return true;
}
Sort answer is here with explanation排序答案在这里有解释
It returns the error because
runner=runner->next->next
can be NULL and you are checking runner->next!=NULL in while conditioning so you have to do a little change In your code In order to get your answer In while condition check forrunner->next->next!=NULL
and get the right answer.它返回错误,因为
runner=runner->next->next
可以为 NULL 并且您正在检查 runner->next!=NULL 在调节时,因此您必须在代码中进行一些更改才能在其中获得答案条件检查runner->next->next!=NULL
并获得正确答案。
Happy Coding :>)快乐编码:>)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.