142. Linked List Cycle II

Assume fast goes 2k steps and slow goes k steps, then they meet. Assume slow goes k=s+mk=s+msteps, fast goes 2k=s+m+ar2k=s+m+ar steps, a>ba > b .

So in math, 2k=2(s+m)=s+m+ar2k=2(s+m)=s+m+ar

Obviously, s+m=nr,s=nrms+m =nr, s = nr-m

Assume n=1,s=rmn=1, s = r-m (fast only need to step one round more than 2*slow)

When slow = fast, let slow continues moving and start move at the same time at the same step until they meet. The steps start walks is the circle meeting.

Last updated

Was this helpful?