思路:
快慢指针法:当快指针与慢指针相遇时,分别从起点,相遇点开始走,相遇即为环入口
/*** 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) {ListNode* slow = head;ListNode* fast = head;while (fast && fast ->next) {slow = slow ->next;fast = fast ->next ->next;if (slow == fast) {ListNode* index1 = slow;ListNode* index2 = head;while (index1 != index2) {index1 = index1 ->next;index2 = index2 ->next;}return index2;}}return NULL;}
};