- 2 weeks ago
Transcript
00:00Next, we will talk about the question. It's called Remove a cycle in a linked list.
00:04In the first question, I have detected the cycle, but I know that if the cycle is in the linked
00:09list, there are many problems.
00:11So, what can I do in this question?
00:13I will remove the cycle from my linked list so that my original linked list can be restored.
00:19It means that if I have made such a linked list in which already exists a cycle and the last
00:28node is pointing to this node.
00:30So, what will I do? The best way to remove the cycle is that you remove this pointer and point
00:36the last node to the null.
00:39So, what do I do? First, I need to find this last node.
00:45And as I find the last node, I will point the next node to the null.
00:53So, what will happen again? My cycle will come into a single linked list.
00:58Now, there will not exist any cycle in the linked list.
01:01So, I have to perform two important steps. The first is to find out the last node.
01:06The second is to find the next node to the null.
01:09So, it will not point the middle node to the middle node.
01:12So, what will I do first? First of all, we can call it an algorithm or approach.
01:21We will discuss the method in which we are going to do this.
01:24And in this method, we have to make notes very good.
01:27In fact, I have to make diagrams in the middle of the diagram.
01:31Because we have to remember the logic from the diagram.
01:33When we revise our placements or internships.
01:36Now, first of all, we will discuss the algorithm or this approach.
01:40I will explain what to do.
01:42I will explain what to do.
01:44Then, we will write code.
01:45Then, I will give you an explanation.
01:48I will explain why we have done.
01:51Mathematically, it will give me a good answer.
01:54In every scenario.
01:56So, first of all, let's talk about our algorithm or approach.
01:59What I am going to do?
02:00I am going to use slow fast approach.
02:01I am going to use something.
02:02If I have to remove some loop or cycle,
02:06then, first of all, we have to detect that cycle.
02:09If it happens, we will remove it.
02:11So, first of all, the step is this.
02:14Find.
02:15Or, I will say, detect cycle.
02:19First of all, we will detect cycle.
02:21Do cycle exist or not.
02:23If I have to know that my cycle exists.
02:25So, what will happen in that point?
02:28Let's take a look at my link list.
02:31Diagram wise.
02:32And here, in the middle of the loop,
02:37my slow pointer and my fast pointer value are the same.
02:42So, what can I say?
02:43I have detected that cycle exists.
02:45Now, I have to remove cycle.
02:47So, what do I do?
02:50The first thing I have to do is
02:53that I have to do the slow value again.
02:57This is my first step.
02:59That means, I have to take my slow back to my head position.
03:04So, I will remove this from here.
03:06And I will take my slow here.
03:10Now, I have to do the second thing.
03:13I have to do the second thing.
03:14I have to do the slow move.
03:16I will move this by plus one.
03:19And I will move this by fast.
03:21I will move this by plus one.
03:23Now, this path I will move this from here.
03:28What I do can do is?
03:29And with slow and fast equals speed.
03:35So, if we move this from there,
03:36Slow is just from there.
03:36goes for a few times, fast is too.
03:43It will always happen like the next time slow and fast will get up on this point.
03:56This point is the point of my cycle creation.
04:01where the cycle is formed, or the last node goes and meets my next node,
04:07the next time I will move my slow and fast.
04:10We will discuss mathematical proof of this later.
04:13Now, remember to remember that where the next time slow and fast meet,
04:17the next time slow and fast is the last point,
04:21which is connected to the middle of the linked list.
04:23We will meet this.
04:25If I can explain it with a little bit of a diagram,
04:28let's assume that my linked list looks like this.
04:31this is where it shows that my cycle is formed.
04:34If I need to detect cycle,
04:36what would I do first of all?
04:37I will show this as a working example.
04:39My slow is here, but my fast is here.
04:42What will slow do in my first time?
04:43Move through by 1.
04:45Where is the fast? Here is the last time.
04:47Where is the slow? Slow is here.
04:49Where is the fast? Fast does it move through by 2.
04:53Where is the slow? Slow is here.
04:55Where is the fast? Fast is here.
04:57and move here. The next time slow came. Slow 1 move here. This was already cut. Fast is now here.
05:04Now there are 2 chalang markers here. I am going to kill all the previous moves.
05:11This will be removed. This will be removed. This will be removed. This will be removed.
05:14Now where slow and fast meet? Meet on this node. As you meet on this node, I have detected my
05:21cycle.
05:21As I have detected my cycle, now I will equal the slow to the head.
05:26So I will remove slow from here and now I will go back to here.
05:31What do I have to do? I will also increase slow and fast.
05:35Slow will increase here. Fast will increase here.
05:40Now slow will increase here. Fast will increase here.
05:45So what happened? Where did they meet?
05:47They both meet where my cycle is the starting point of my cycle.
05:55Which exists under linked list.
05:57So what can I say?
05:58The last node was my last node.
06:00The next node is the next node.
06:05The next node is slow and fast.
06:07So what can I say?
06:09This is my last node.
06:10Which is my last node.
06:16This is also previous of fast.
06:21Think about it.
06:23Why did it happen?
06:29If my slow and fast meet the second time, then this node will be my last node.
06:35Can I say last node is the previous node of fast? Why?
06:38Because fast is here and where is the previous node?
06:42So the last node of fast is the last node.
06:45So what do I do?
06:46I have started playing one-one move from one-one.
06:50Then I will start a loop.
06:51The third step is to start a loop.
06:54When will I start a loop?
06:55Until my slow will not be equal to fast.
06:58Because I know that we will meet again.
07:01And where will we meet?
07:02We will meet this always on this point.
07:06And when will I start a loop?
07:07Until their value will not be equal.
07:09I will increase slow by one.
07:13And fast will also increase by one.
07:16So my step B will be equal to this loop.
07:22And with slow and fast I have another variable tracker.
07:25That is my previous.
07:27What will be the value in the beginning?
07:28What will happen?
07:29What will happen?
07:29Just like my fast 1 has already increased,
07:31I mean, if I have nodes in this way.
07:34And my fast is pointing at this point.
07:36So the first node is pointing at this point.
07:37Just like my fast points at this point,
07:38My previous node will point at this point.
07:42We will track previous node.
07:43So I will initialize previous node.
07:47And with null,
07:49As I go inside,
07:49As I go inside fast value will increase,
07:51My previous value will be equal to my fast.
07:54So in the last node,
07:55When this loop is finished,
07:57I will get slow and fast here.
07:59After that,
08:00What will I do?
08:01I will make the next node null.
08:03Because what is my previous node?
08:05My previous node is my last node.
08:07And what did I do?
08:08I had to remove the cycle.
08:09Before I had to find out the last node.
08:12I had to find out the previous node.
08:13Then I had to find out the previous node.
08:14I had to find out the next node.
08:17Then I had to find out the previous node.
08:18I had to find out the next node.
08:19So I will find out the previous node.
08:19So this will be a code for any link list.
08:23I will remove the cycle.
08:24I will give it a short summary.
08:26After that, we will write code.
08:27First of all,
08:28I will detect the cycle.
08:29As I have done in the previous question.
08:31After that,
08:32I will add the slow node.
08:33So the value of the head is now re-initialized.
08:37Now I will run a loop.
08:38I will track the previous node.
08:41I will initialize the value of the previous node.
08:51With the previous node,
08:53I will point the previous node to the previous node.
08:55I will point the previous node to the previous node.
09:08And remove the cycle of the previous node.
09:10What is the way to remove the last node?
09:10What is the way to remove the last node to the next node?
09:12I will create a null.
09:13So that this cycle
09:14will stop something here.
09:16And the node,
09:18the connection,
09:19will be cut.
09:19ahead of time and make the interest.
09:20This is called to write code.
09:21It's a great day.