/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function EntryNodeOfLoop(pHead)
{
// write code here
let head=pHead;
let count=0;
while(head){
if(head.flag==true)break;
head.flag=true;
head=head.next;
count++;
}
while(count--)pHead=pHead.next;
return pHead
}
module.exports = {
EntryNodeOfLoop : EntryNodeOfLoop
};
和上一个检查是否有环的算法题一样,给每个节点设置flag标记,当遇到标记的时候就说明到环了,记住这个count值,再遍历一遍找到这个节点即可



京公网安备 11010502036488号