LeeCode-Linked List Cycle

Given a linked list, determine if it has a cycle in it.

 

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 bool hasCycle(struct ListNode *head)
 9 {
10     struct ListNode *p1;
11     struct ListNode *p2;
12     
13     p1=head;
14     p2=head;
15     
16     if(p1==NULL||p1->next==NULL)
17         return false;
18     
19     while(p2->next!=NULL&&p2->next->next!=NULL)
20     {
21         p1=p1->next;
22         p2=p2->next->next;
23         
24         if(p1==p2)
25             return true;
26     }
27     
28     return false;
29 }
原文地址:https://www.cnblogs.com/vpoet/p/4660476.html