class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *fast = head;
ListNode *slow = head;
while(fast&&fast->next){
fast = fast->next->next;
slow = slow->next;
if(slow == fast){
ListNode *index1 = fast;
ListNode *index2 = head;
while(index1 != index2){
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return NULL;
}
};
环形链表II
最新推荐文章于 2024-02-06 14:30:00 发布