Article directory
foreword
1. What is a linked list ring?
As shown in the picture:
提示:以下是本篇文章正文内容,下面案例可供参考
1. Topic
Two, problem solving
1. think
slow moves one at a time, while fast moves two at a time:
Move slow first each time, and then move fast
Until slow==fast, the linked list has a ring
2. Realize
code show as below:
bool hasCycle(struct ListNode *head) {
struct ListNode *slow=head,*fast=head;
while(fast&&fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(fast==slow)
{
return true;
}
}
return false;
}