/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/*这是到数学技巧题目,具体参考自:https://github.com/soulmachine/leetcode*/
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 *slow2(head);
while(slow !=slow2){
slow2 = slow2->next;
slow = slow->next;
}
return slow;
}
}
return nullptr;
}
};
LeetCode之Linked List Cycle II
最新推荐文章于 2022-10-29 14:39:35 发布