思路: 数学推导,第一次相遇之后,left回到head,left和right 同时同速往后遍历,相遇时则为循环节点。推导见此处
http://www.cnblogs.com/x1957/p/3406448.html
http://www.cnblogs.com/ballwql/p/3676843.html
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (!head) return head;
ListNode *left = head;
ListNode *right = head;
while (right){
left = left->next;
if (right->next)
right = right->next->next;
else
break;
if (right&&left == right)
break;;
}
if (!right || !right->next) return nullptr;
left = head;
while (left != right){
left = left->next;
right = right->next;
}
return left;
}
};