标签:coder src node play alt 快慢指针 upload images div
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL){
return 0;
}
ListNode* slow = head;
ListNode* fast = head;
while(fast != NULL && fast->next != NULL){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
break;
}
}
if(fast == NULL || fast->next == NULL){
return NULL;
}
slow = head;
while(slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
标签:coder src node play alt 快慢指针 upload images div
原文地址:http://www.cnblogs.com/Czc963239044/p/7076962.html