β I don't understand what I did wrong(list)
a list is a circle if its last node points at a previous node.
I tried writing a function that can tell whether a given list is a circle, it works except for the last 3 check ups where it loops forever.
I provided pics of my code and the running code.
*ListFromString points the last node of a given list of int at the first one)
*I only had problems with the code inside the for loop
![No description](https://cdn.answeroverflow.com/1197919932499894362/image.png)
![No description](https://cdn.answeroverflow.com/1197919932797681696/image.png)
6 Replies
I believe the problem must be caused by the circle part of the list repeating itself over and over but curr1 and curr2 should meet and I checked it on paper and it seemed to work
so I'm not sure what the problem is
a list is a circle if its last node points at a previous node.
Do you mean if it's last node points at the first node?
Or just if it points at any previous node?any previous one
in the for loop it'd be 1->0 -> 0 ->...
and it's still considered a circle
You can use Floyd's cycle-finding algorithm, it would be something like
ty
The idea behind the algorithim is that
n1 n2
3 iteration 0 -> 1 -> 3 -> 1 ^ ^ n1 n2 4 iteration 0 -> 1 -> 3 -> 1 ^ n1 n2 Means it's a full cycle
n1
moves faster
So for example
0 -> 1 -> 3 -> 1
^
n1
n2
1 iteration
0 -> 1 -> 3 -> 1
^ ^
n2 n1
2 iteration
0 -> 1 -> 3 -> 1
^ ^n1 n2
3 iteration 0 -> 1 -> 3 -> 1 ^ ^ n1 n2 4 iteration 0 -> 1 -> 3 -> 1 ^ n1 n2 Means it's a full cycle