/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head, *slow = head;
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
ListNode* curr = head;
while (curr != fast) {
curr = curr->next;
fast = fast->next;
}
return curr;
}
}
return nullptr;
}
};
环形链表II-链表142-c++
于 2021-03-24 16:19:13 首次发布