对于指针还是非常不熟悉!
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
/*set<ListNode*> node_map;
while(head)
{
if(node_map.find(head) != node_map.end())
return true;
else
node_map.insert(head);
head = head->next;
}
return false;*/
ListNode *slow = head;
ListNode *fast = head;
if(head == NULL || head->next == NULL)
return false;
while(slow->next && fast->next!=NULL &&fast->next->next)
while(slow->next && fast->next->next) 错误member access within null pointer of type 'struct ListNode'
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}
};