算法题 判断链表是否有环,求环的入口点及环长



a为环入口点到头结点的路程,x为相遇点到环入口点的路程。我们假设slow指针走过的路程为s,那么fast指针走过的路程则为2s,假设环长为c。且有

2s = s + nc

s = a + x

有上述式子,我们可以得到

a + x = nc

a = nc – x

a = (n – 1)c + c -x

如果在头结点位置和相遇点位置分别再派出两名跑步选手,并且他们都每次只跑一步,好像会在环的入口点相遇啊!




https://www.cnblogs.com/springfor/p/3862125.html

https://blog.csdn.net/xiaxia__/article/details/19356861

猜你喜欢

转载自blog.csdn.net/qq_35546040/article/details/80135083