题解 | #判断链表中是否有环#C++暴力解法判断是否有环
判断链表中是否有环
https://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9
/**
* 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)
{
ListNode*p=head;
ListNode*q=head;
if(p==NULL)
{
return 0;
}
while(p && q)
{
if(p->next && p->next->next)
{
p=p->next->next;
}
else
{
return 0;
}
if(q->next)
{
q=q->next;
}
else
{
return 0;
}
if(p==q)
{
return 1;
}
}
return 0;
}
};
