Hi everyone, let's do this question.
You are given the head of a linked list.
Check if there is a cycle and if yes, return the node where the cycle begins.
If there is no cycle, return null.
There is a cycle in a linked list.
If there is some node in the list that can be reached again by continuously following the next pointer,
do not modify the linked list.
So this is the question over here.
Now for example, let's say this is the linked list which is given to us.
So we will be given the head which is one.
Now if you can just keep going next.
So we go next we come over here again.
We go to the next.
So we are following the next pointer.
We come over here and if we again follow the next pointer we are coming back to this node over here.
So that means there is a cycle in the linked list.
Now in this case we have to return the node where the cycle begins which is this node over here.
That is this question over here.
Now let's go ahead and write a few test cases.
Now let's say the linked list which is given to us is this one over here.
Now in this case you can see there is a cycle over here and it's starting over here.
So we have to return the node three over here.
Now if the linked list looks something like this that is one going to two going to three.
And then that's pointing to null.
So there is no cycle.
And in that case we have to just return null.
And let's write a few more test cases.
Let's say the linked list has just one node.
Now over here.
Also we have to return null because clearly there is no cycle.
And if the linked list is just null, that is there is no node even at the head.
Then also we have to return null because clearly there is no cycle.
Now in the next video, let's try to think how we can solve this in an efficient manner.