一:题目
二:思路分析
三:上码
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast != NULL && fast->next != NULL && fast->next->next != NULL) {//这里选用快指针
fast = fast->next->next; //因为快指针是最快到达尾结点的
slow = slow->next;
//相遇了
if (fast == slow) {
ListNode* index1 = fast;
ListNode* index2 = head;
while (index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return NULL;
}
};
我下次一定好好弄思路 ipad随手写,然后就是随手导出的 啊啊啊啊啊呜呜呜呜呜