LeetCode 142. 环形链表 II
/**
* 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) {
if(!head) return NULL;
auto i=head,j=head;
do
{
i=i->next;
if(j->next&&j->next->next) j=j->next->next;
else return NULL;
}while(i!=j);
j=head;
while(i!=j)
{
i=i->next;
j=j->next;
}
return i;
}
};
有帮助的话可以点个赞,我会很开心的~