第一次做网站不知道建站官网
环形链表II
首先,先判断有没有环,像物理相对速度一样
只要 相对速度为1 那么快指针绝对会在环里追上慢指针,最后x 和z 的距离其实最后两个index总会相遇,相遇的点就是入口
class Solution {
public:ListNode *detectCycle(ListNode *head) {ListNode*slow =head;ListNode*fast=head;while(fast&&fast->next){fast=fast->next->next;slow=slow->next;if(fast==slow){ListNode*index1=slow;ListNode*index2=head;while(index1!=index2){index1=index1->next;index2=index2->next;}return index1;}}return NULL;}
};