142. 环形链表 IIc
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* slow=head,*fast=head;
while(fast && fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast){
while(head!=slow){
head=head->next;
slow=slow->next;
}
return slow;
}
}
return NULL;
}
结果:

浙公网安备 33010602011771号