Detect start of a loop in linked list
Given a linked list, implement an algorithm which returns the node at the beginning of the loop.
This post is a follow-up of
- Detect a loop in cyclic/circular linked list.
- Loop Length in cyclic/cicular 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 cyclic/circular linked list.
p1at the head of the linked list, and keep
p2at the same location where
p2one step at a time, when they will meet again it's the beginning of the loop.