00:00Now I will create a new function which I will name Public, Static, Void and the return type will be
00:07RemoveCycle
00:10In the first step of the remove cycle, the second step will be Find Meeting Point
00:20The third step will be RemoveCycle. How will the cycle remove?
00:24The next step will be RemoveCycle when I will create the next step.
00:28First, let's detect the cycle. I will write the same code. I will initialize slow with head
00:34And I will initialize fast with head
00:40Until my fast value is not equal to null
00:44Or until my fast next is not equal to null
00:48Until I have to create slow and fast
00:59Next
01:00What do I do every time? I will check that if my fast is equal to slow
01:06Then what do I do now? I will not return
01:09If the cycle exists, I will remove the cycle
01:13So if it exists or not, I will create a variable
01:15Flag
01:16Or I will call it Exist
01:20Exist before I call it 0
01:21Exist means that the cycle exists not
01:24In fact, I will name cycle
01:27And I will create Boolean type
01:32Boolean cycle is false or equal
01:34That means that the cycle doesn't exist
01:36But as fast and slow is equal, I will do the cycle
01:40True
01:40And here I will break
01:42I will not add further
01:44I will change my fast position
01:45I will change the slow to head
01:48In this second step
01:49But I will start with the fast
01:50I will start with the first time
01:53So I will not add further
01:56I will break
01:58Last, I will check that
01:59If my cycle's value is false
02:03In this case, the cycle doesn't exist
02:05So I will return
02:05To my code
02:07If it exists, what do I do now?
02:10I will find out the meeting point
02:12So I will initialize my slow
02:15With the head
02:16And now I will run a loop
02:18Until my slow value is equal
02:21What do I do now?
02:24I will track my previous
02:25So I will take a previous node
02:26With the previous node
02:27I will initialize
02:28With the null
02:28Every time
02:29My previous value will be stored
02:32Then I will increase the slow
02:34By one
02:35Slow is equal to slow
02:35And I will increase the fast
02:37By one
02:38Fast is equal to fast
02:39Next
02:40This is my loop
02:41I have started
02:42In this loop
02:43When we go out of this loop
02:44Then the fast value will be equal
02:47And in my previous node
02:48What will store my last node?
02:51I will null the loop
02:53I will null the loop
02:54So I will null the previous node
02:57I will null the loop
03:00I will null the loop
03:01I will null the loop
03:02So I will null the loop
03:03I will null the loop
03:03I have written a complete code
03:04For my remove cycle
03:06So the first step
03:08To detect cycle
03:09I have written the same code
03:10I have written as the previous
03:11The extra variable
03:12This is the cycle
03:13To track the cycle
03:15To track the cycle
03:15Or not
03:16After I have found out
03:18The meeting point
03:19As it is
03:20Slow and fast
03:21Equal
03:21The second time
03:22Meet
03:23And then I have null the last node
03:24I have null the next node
03:25I have null the previous node
03:27I have null the previous node
03:28Let's do this
03:30Now
03:30In the main function
03:31I have made a link list
03:33Which is in the cycle
03:33Which is in the cycle
03:35So
03:351 to 2
03:36To 3
03:37To 3
03:37To 3
03:37To 1
03:39To 1
03:39To 1
03:42To 1
03:43To 1
03:44To 1
03:44To 2
03:47To 3
03:48To 1
03:49To 1
03:49To 1
03:49I will save it and now I run it.
03:54There is an exception in which line?
03:57There is an exception in 273 line.
04:00In my previous line, there is a null value.
04:04What is the exception saying?
04:05We cannot assign next because your local variable is null.
04:10In previous line, it is null.
04:11The previous value has not changed.
04:13Why did this happen?
04:14In the remove cycle, I have initialized slow and fast
04:17I have initialized with the head.
04:19In the cycle, I have added false.
04:22As the cycle is true, we will go out.
04:26Let's check all of our variables.
04:28I have also checked fast and next.
04:32After that, I have to take slow back to the head.
04:35I will initialize my previous value from null.
04:38Until my slow value is not equal to fast.
04:41My loop runs.
04:43The previous value is equal to fast.
04:46I have made slow and fast.
04:49What will I do?
04:50I will analyze this situation through the diagram.
04:55What did I do?
04:57I have made a link list in my main function.
05:01Where I touched the corner case.
05:03How did I touch the corner case?
05:04I have made a link list in which the head is one stored.
05:08Then it is two points.
05:09Then there is a third node, three.
05:11And the next node is equal to my head.
05:14What did I do?
05:16What did I do?
05:16What did I do?
05:17What did I do?
05:18I had slow before.
05:19And here I was fast.
05:21So in the code, I did slow before.
05:23I increased slow and fast.
05:25I increased slow here.
05:27Then slow is slow.
05:29So slow is here.
05:30Where did I do?
05:32Fast is here.
05:33Fast is here.
05:38So I increased fast.
05:42My value is here.
05:44So my value is here.
05:46This is fast here.
05:46The first time when I meet the link list.
05:50The first time I meet the link list.
05:51I have made the link list in my main function.
05:53He has used the head.
05:55So the both of the head meet.
05:58So this is a corner case. Usually we have cases in the link list.
06:03In the link list, there is a link list in which the last node is connected to a node.
06:10But here directly, the last node is connected to the head node.
06:14It's like next here, next here.
06:17Our contests or platforms usually don't have a full cycle form.
06:22We have to write the code in the middle of the cycle.
06:26We are going to run our code in the middle.
06:30So I have created a wrong link list in my main function.
06:33I will create a right link list in there.
06:37So I am going to change my link list in this scenario.
06:41In the link list, first I will create a node.
06:44Temporary is equal to new node of 3.
06:48Or new node of 2.
06:49This is temporary, it will become 2.
06:51And I will connect with temporary.
06:55Then 3 and 3 will be connected with temporary.
06:59It's basically like I have connected 1 to 2 and 2 to 3.
07:02Now I am going to connect with 3 to 2.
07:04It's not directly with 1.
07:06In this case, if I save and run my same code.
07:09I will run my same code.
07:10So I will have output there.
07:12First it will be true.
07:13Because first my cycle exists.
07:15Then the output will be false.
07:17In this scenario, I will never connect with the head.
07:21In that case, what will I always have?
07:23In that case, I can use this same code.
07:26In fact, you will go to different coding platforms.
07:28You will go to the lead code.
07:30You will go to the lead code.
07:30Or you will go to another platform.
07:32Usually there are cases where the head is not connected with the head.
07:35So I will write the same code there.
07:38In this case, I will write the same code.
07:39In this case, I will write the same code.
07:40In this case, I will write the remove cycle.
07:41When I will remove the link list in my cycle.
07:45I will write the same code as the same code that I will write the same code as well.
07:50And then I will null the last node.
07:52What is the mathematical explanation after that?
Comments