How to find if there is any loop in the list of links using two pointers?

Possible duplicate:
How to determine if a linked list has a loop using only two memory locations.

Hi, I was asked in an interview that I can find the loop in the list of links using only two pointers.

I did the following:

1) each time find the center of the list of links

2), repeating this at the end, both pointers will point to the same node, if you do not specify the same node, and find zero, then there is no loop in the list of links.

is there an effective method for this ...?

thanx in advance.

+3
source share
3 answers

, Floyd, " ". , ( "" ) node , ( "" ) . , "" , "" . , node. - , node .

, , . , . () - node .

+9

1 node , 2 . node , , , . .

, .

+2

- . node . . NULL == second, .

+1

Source: https://habr.com/ru/post/1757611/


All Articles