141. LeetCode circular linked list given: runtime error: member access within null pointer of type 'struct ListNode' ...

Original link: http://www.cnblogs.com/SuzanneHuang/p/10454155.html
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     bool hasCycle(ListNode *head) {
12         if(!head->next||!head)
13             return false;
14      ListNode *pre=NULL,* cur=head;
15         
16         while(cur)
17         {
18             ListNode* tmp=cur->next;
19             cur->next=pre;
20             pre=cur;
21             cur=tmp;
22         }
23         return (pre==head);
24     }
25 };

This is the error code, then find out the reasons found to be null pointers determine the order on line 12 in question, should be if (! Head ||! Head-> next)

Reproduced in: https: //www.cnblogs.com/SuzanneHuang/p/10454155.html

Guess you like

Origin blog.csdn.net/weixin_30500289/article/details/94812670