题目描述
分析
参考 环形链表 快慢指针问题 但是这一题的参数是什么鬼???????????????
C++
/**
* 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) {
bool cycle = false;
ListNode* fast = head;
ListNode* slow = head;
while(fast && fast->next!=NULL){
fast = fast->next->next;
slow = slow->next;
if(fast == slow){
cycle = true;
break;
}
}
if(!cycle){
return NULL;
}
slow = head;
while(fast != slow){
fast = fast->next;
slow = slow->next;
}
return fast;
}
};