简体   繁体   English

'struct ListNode' 类型的空指针内的成员访问

[英]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 for runner->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.

相关问题 运行时错误:“ListNode”类型的 null 指针内的成员访问 - runtime error: member access within null pointer of type 'ListNode' 螺旋矩阵挑战:“ListNode”类型的 null 指针内的成员访问 - Spiral matrix challenge: member access within null pointer of type 'ListNode' 链接列表中的运行时错误:“ListNode”类型的 null 指针中的成员访问 - Runtime error in Linked List :member access within null pointer of type 'ListNode' 使用struct作为私有成员在链接列表类中定义ListNode - Using struct as private member to define ListNode within link list class 为什么我会收到此运行时错误:null 类型“Solution::node”(solution.cpp) 指针中的成员访问 - Why am I getting this runtime error: member access within null pointer of type 'Solution::node' (solution.cpp) C ++:指向结构内成员函数的指针 - C++: Pointer to Member Function within Struct 调用对象类型“ ListNode *”不是函数或函数指针 - Call object type 'ListNode *' is not a function or function pointer 如何访问指针结构的成员变量? - How to access a pointer struct's member variables? 如何访问作为指针参数的指针传递给 function 的结构成员? - How to access a member of a struct which is passed into a function as pointer to pointer parameter? 为什么我在 null 指针错误(链表)中获得成员访问权限 - why do I get member access within null pointer error (linked list)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM