3 ms·
Can you just store the first reference as a local variable and then compare each time you move to a new node? Or to save the comparisons make the thing the fir
by quickthrower2 4y ago
Can you just store the first reference as a local variable and then compare each time you move to a new node?
Or to save the comparisons make the thing the first node points to have a stop flag of some sort.
- mdavidn 4y agoThat approach works only when the first node is included in the cycle. Other cycles are possible.
- Jtsummers 4y agoConsider this kind of cycle: Head -> Node1 -> Node2 -> Node3 ^ | +-----------------+ If it's a circular list then your option would work, but not all cycles will produce circular lists.
- quickthrower2 4y agoThanks. This seems so obvious now! Always draw it out I guess.