Skip to content

Latest commit

 

History

History

linked-list-cycle-ii

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

More work after Linked List Cycle I

To find where the cycle begins, we can use a fast-slow pointer.

The faster one walks length of the cycle steps before the slower one, and then, the slower one start to walk. Where the faster one and slower one meet is the point that the cycle begins.

Measure the length of the cycle

After the fast-slow runners encounter, let them go on running until they will meet at some point.

If slow-runner walks 1 step each loop, when they meet again, the slow-runner will walk length of the cycle steps.

So, code might be like


loop until fast meets slow

   if not meet first time
     length = length + 1