bool hasCycle(struct ListNode *head) {
int poi=0;
if (head == NULL || head->next == NULL) {
return false;
}
struct ListNode *slow = head;
struct ListNode *fast = head->next;
while(slow!=fast){
if(fast==NULL||fast->next==NULL){
return false;
}
else{
slow=slow->next;
fast=fast->next->next;
}
}
for(int i=0;;i++){
head=head->next;
poi++;
if(head==slow) {
printf("%d",poi);
break;
}
}
return true;
}
leetcode 141. 环形链表
于 2024-07-08 12:10:17 首次发布