 What's up guys my name is Michael and welcome to my YouTube channel today we are going to go over cycle detection on hacker rank. So basically if you have a link list of 123 and then then to here in this case what our function is trying to do is we're trying to return true if we have a cycle and false if we don't. So when we mean my cycle means that when we traverse through this list we get back to a node that we already seen before. So if I've had I go to two I go to three and then three I go back to two I already seen this to therefore there's a cycle it means there's like a loop here. So you can have like multiple cycles I'd say I had like one two and two goes back to one this is a cycle because if I were to traverse through one I would go back to one and I've already seen one so that's a cycle. So basically any like loops inside your link list so if I'd like three this goes back to here this is a cycle because if I go to one two and then three three goes back to one and that's cycle. Okay so how do you do this problem simplest way is actually just to have two pointers one called fast and one slow. So let's say we have our cycle right here I'm going to have a fast pointer and a slow pointer. So this is going to be slow and then we're going to have fast right here also. So fast is just going to go twice as fast. So what's going to do is on the first iteration slow is going to go to the next node right only going by once. So this is going to be slow and then fast is going to go twice as fast so fast is going to go dot next dot next so fast is going to go here right jumps twice as fast goes originally is more here goes to here now now after that slow is going to go to the next node so slow is going to go here and fast is going to jump twice as fast so fast is going to jump goes to two and then it's going to jump again back to three so fast is going to stay where it is. So then if if our slow node is ever equal to the same node as a fast node it is basically it would have a cycle. And this always works because if you were to always traverse twice as fast sooner or later you you're going to get to the same node. If there is a cycle right if we're traversing one time down the list and the second time in the faster pointer is going twice as fast if there is a cycle it's going to meet somewhere in the middle right because we're just going we're looping backwards right if it's not if it reaches the end and it never meet met each other then that means that there was no cycle. So here's a code for it we have slow and then we have fast pointing the head while fast is not equal to null and slow is not equal to null and fast next is not equal to null because fast since fast is going twice as fast we have to check if fast next is not equal to null. Then what we're going to do is we're going to set slow is going to equal to slows next and we're going to set fast is going to equal to fast next dot next then if fast is ever equal to slow we return true otherwise when we reach the end we just return false. So yeah that's pretty much the gist of this code it's not that much difficult another way you could do is you could just hash every single pointer every node you see and see if you've seen it again. If you've seen it again then yeah then that means that there's a there's a loop yeah that's pretty much the gist of this rate comps grab I'll check you guys later peace.