I forgot what I asked at that time, because I talked a lot and my memory is not good.
It was probably "How to judge whether a chain has a loop"
I only vaguely remember the meaning...
Thank you for your help Let me correct the question. I mainly want to know what is being asked.
This is a bit of a tough question
a.next
isb
b.next
isc
c.next
isa
...
If you execute the following loop
Then it will be an infinite loop, and temp will be assigned as follows:
a => b => c => a => b .....
Suchabc
constitutes a cycleYou can refer to circular queue and ring linked list.
So how to judge?
Since he said he wants me to judge, follow the steps above.
Recursion
ScreenShot
(After I finished writing, I realized I made a mistake and rewrote it... == Sorry)
This question is a very classic algorithm question. The most classic method is to use the
fast and slow pointer method
. For specific questions, you can move to leetcodeSimply put, define the fast pointer and the slow pointer. The fast pointer takes two steps at a time, and the slow pointer takes one step at a time. If the two of them can meet, it means there is a cycle.