Find loop length in a cyclic/circular linked list
A linked list
This post is a follow-up of
- Detect a loop in cycliccircular linked list.
I recommend reading those posts first, as the following code uses the methods from it.
- Find the loop, using the same logic Detect a loop in cycliccircular linked list.
p2one step at a time keeping
p1at a fixed location.
- Use a
loopLengthvariable to keep track of loop length.
p1 === p2return the loopLength.
Watch the following video to understand the Floyd's cycle-finding algorithm!