做网站需要什么知识,团购网站app制作,批量做单页网站,交通建设集团蓝商分公司网站环形链表II
首先#xff0c;先判断有没有环#xff0c;像物理相对速度一样
只要 相对速度为1 那么快指针绝对会在环里追上慢指针#xff0c;最后x 和z 的距离其实最后两个index总会相遇#xff0c;相遇的点就是入口
class Solution {
public:ListNode *detectCycle(List…环形链表II
首先先判断有没有环像物理相对速度一样
只要 相对速度为1 那么快指针绝对会在环里追上慢指针最后x 和z 的距离其实最后两个index总会相遇相遇的点就是入口
class Solution {
public:ListNode *detectCycle(ListNode *head) {ListNode*slow head;ListNode*fasthead;while(fastfast-next){fastfast-next-next;slowslow-next;if(fastslow){ListNode*index1slow;ListNode*index2head;while(index1!index2){index1index1-next;index2index2-next;}return index1;}}return NULL;}
};