I have brushed this question before, I do not know how many times, it must be more than a month since the last time to brush this question, today I read the captain’s first lesson to rewrite,

Method 1 fast and slow pointer

The first step is to write the following based on my previous understanding without looking at the answer

Next, I said while (cur && cur.next). So when I started the while loop, I was thinking, well, the first thing I wanted to do was loop that list, and it must have been a cur, but when I did that, To judge fast, I changed line 18 to while (cur && fast. Next)

I read the newspaper error and realized that head was not called short, so the above error occurred. So I added the first line of code to call short at the beginning, and the error is still reported as follows

This error says fast. Next. Next is null, so I think I should add fast. Next is null, but I still say fast is null, I think I can’t see where is wrong, so I can only read the answer written before. Loop while (fast $fast.next) while (fast $fast.next) To summarize this, according to my current understanding, the method of fast and slow Pointers is correct, but when writing code, while should loop who, I thought wrong. Forcing myself to follow the correct code to understand, loop fast, fast takes two steps at a time while will loop forever, instructing return true, forcing termination of the while loop. Now, if you think about it, if there were no loops, you wouldn’t keep going through the while loop and there must be some node that’s null, and if there were, it would keep going through the while loop unless the fast and slow Pointers were equal so the fast Pointers took one more turn than the slow ones. That is, the existence returns true to stop just when the fast pointer has made one more turn than the slow pointer.

Fast and slow pointer that I am poor while should loop who is not too clear, and then went to see the teaching assistant to speak of the problem, fast and slow pointer thinking the same but why the loop fast did not say, I went to see the great Saint of the teacher, suddenly thought of method two this problem solving idea, relatively simple, while into no error. So why loop fast instead of loop slow is because fast goes fast, it updates and it encounters null, and why is it not loop cur, but maybe I got it mixed up with some other list title that I’m not going to go too far with

Method 2

It’s perfectly understandable that you store each value that you loop through, and as you loop through the next node, you check the next node to see if you’ve stored it before, and if you’ve stored it, it’s a circular linked list.