class Solution {
public:
ListNode *detectCycle(ListNode *head) {
int flag = 0;
ListNode *slow = head, *fast = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
//首次相遇点
flag = 1;
break;
}
}
if(flag == 1){
ListNode* curNode = head;
while(curNode != slow){
curNode = curNode->next;
slow = slow->next;
}
return curNode;
}
return nullptr;
}
};
LetCode Hot100 142.环形链表II
最新推荐文章于 2025-05-01 13:25:47 发布