- 2 weeks ago
Transcript
00:00Hi everyone and next question which we will talk about is to detect a loop or a cycle in a
00:05linked list.
00:05Now we have seen any linked list in which we have never seen any cycle.
00:09What does this mean?
00:10This means that our linked list which we have worked on,
00:14was seen in this way in which the last node points the null.
00:20But a link list in which a loop or a cycle,
00:24my last node connects to a middle node.
00:28If I take an example of this link list,
00:31this is my last node,
00:35it seems to point the next node to the next node.
00:38If I travel on a normal link list,
00:41I will go to the head, then the other node,
00:44then the other node, then the other node,
00:46then finally I will reach the null and I will know that my link list is finished.
00:50But with a loop link list, there is a problem.
00:54If I start from the head,
00:56I will go to the next node,
00:58then this node,
00:59then this node,
01:00then this node,
01:00then this node,
01:01then this node,
01:03and now I will go to the next node.
01:06So I will not go to null.
01:07After this node,
01:08I will go to the middle node.
01:10and this node,
01:12I will go to the next node.
01:15So I will go to the next node.
01:18Then we don't get any end of the link list. If we don't know that the link list is becoming
01:24a cycle or not, then our code is getting stuck in this infinite loop.
01:31In cases, it is important to know that the link list is actually formed in a cycle or loop.
01:37Now, if there is a cycle formed in a link list, what do we need to use an algorithm?
01:43We give the name of Floyd's Cycle Finding Algorithm.
01:51We don't need to remember the name of this algorithm. We need to remember the logic of this algorithm.
01:57We mostly don't ask the name of the algorithm. Most people ask how to find out the cycle.
02:04So, we need to know the method.
02:05So, we need to know the method. For the method, we use the Floyd Cycle Finding Algorithm.
02:10For this, we are going to use two pointers.
02:13And the approach we will use in the previous questions.
02:17For this, the two pointers we will use, one will name slow and one will name fast.
02:22So, what we will use in this approach is our slow and fast pointers.
02:28We have used to find out the midpoint of any link list.
02:33Now, we will use a cycle.
02:36In slow and fast pointers, we use slow pointers.
02:39It is always 1 step.
02:42And our fast pointers are always 2 steps.
02:47So, we are taking advantage of this thing.
02:49What are we going to do?
02:51I am going to tell you that we will do this algorithm.
02:53Then, I will show you a mathematical logic.
02:56What are we going to do?
02:57We will initialize the slow pointers with the head.
03:00And we will initialize the fast pointers with the head.
03:03Then, we will move slow by one.
03:06So, I will move slow to this position.
03:10Then, I will move fast by two.
03:12So, I will move fast by two.
03:13And I will move slow to this position.
03:14If I move slow by one, I will come here,
03:18and fast will come here two positions.
03:20Or here.
03:21Then, I will move slow to another position.
03:24Then, I will move slow by two positions.
03:25And fast will come here two positions.
03:28In this position.
03:29Here, we have something to notice.
03:32That by beginning, there was an difference in slow and fast.
03:35What was the difference in slow and fast?
03:36How was the distance?
03:37During the初.
03:38When zero moves were.
03:40So, the distance in both.
03:41the distance between them was equal to 0. What can I say? Just like one move,
03:49I jumped slow here and I jumped fast here. What was the distance between them?
03:55The distance between them was 1. Just like one move, the distance between them was 1.
04:00Just like they jumped the 3rd. When my slow came here and my fast came here,
04:06the distance between them was 2. When they took 2 moves, the distance between them was 2.
04:16When they took 3 moves, my slow came here and my fast came here.
04:22The distance between them is 1, 2 and 3. When they took 3 moves, the distance between them is 3.
04:34We will use this to prove that my algorithm works.
04:38But I understood this logic that when one move is not done, the distance between them is 0.
04:43When they took one move, the distance was 1. When they took one move, the distance was 1.
04:47When they took one move, the distance was 2, then the distance was 2.
04:51Then the next move was 3, then 4, then 5.
04:54The distance will increase between them. Now let's go back to slow and fast.
05:04Now click 2 with let's go back.
05:10Now look at this step then the distance was 2.
05:16Now my slow is here. Fast has two channels, so it will come here and here and here. Fast is
05:23here.
05:24Slow has two channels, so my slow is here and here. Fast has two channels, so it will come here
05:31and here.
05:32Fast also has here. What do I do? The previous moves I delete my first time.
05:38What happened in the final move? The slow and fast are on a node.
05:46That means, I have the link list in the cycle.
05:51The cycle is on a node.
05:53It is on a slow and fast meet.
05:56And as I am in a cycle or in a link list, I am on a slow and fast meet.
06:03If you meet them, it means that my link list exists in my cycle.
06:08So, as both pointers and variables meet again,
06:12the value of slow will be equal to fast.
06:16The cycle is true.
06:19That means that my link list exists in my cycle.
06:21And if slow and fast meet, I will return false.
06:25That means that my link list doesn't exist in my cycle.
06:28I will compare this if I have a normal link list.
06:31If I have a normal link list in which there is no cycle.
06:34It ends with null.
06:36So, what happens?
06:37My slow is here and then my fast is here.
06:41In the beginning, if slow is one move, then it will come here.
06:44Fast is two moves, then it will come here.
06:47Then, slow is one move, then it will come here.
06:50Fast is two moves, then it will come here.
06:52Now, slow is one move, but fast is two moves.
06:55It will come here because there is no next.
06:57That's why we have to stop here.
06:59In this case, when I have a single link list,
07:03there is no loop.
07:04In this case, I have to stop at one point
07:06because there is no place for fast.
07:09And in these cases,
07:10sometimes slow and fast,
07:13never meet again.
07:15In the beginning, both positions are ready,
07:16so they meet again.
07:18But after that, they don't meet again.
07:19But in this case, the link list is always slow and fast.
07:26And as they meet again,
07:27and as they meet again,
07:28you will use the point later.
07:30As they meet again,
07:32what happens?
07:33In this case, the link list is cycle form.
07:36Now, what is proof?
07:37As we meet again,
07:38I know that it is cycle form.
07:41Mathematically,
07:41we can understand this.
07:42In this case,
07:44I have to understand the diagram
07:44that why not in linear
07:45and why not in loop
07:47and why not in loop
07:48We can understand this.
07:50Mathematically,
07:50we can understand this.
07:51As we move more.
07:53As we move more.
07:54What happens?
07:55The distance will increase.
07:56First, the distance was 0,
07:58then 1,
07:58then 2,
07:59then 3,
07:59then 4,
08:00then 5,
08:00then 6,
08:01then 7.
08:01In this case,
08:03one day,
08:04the distance is equal to D.
08:06What can I say?
08:07If my link list looks like
08:09something like this,
08:09which is cycle form.
08:11It can be small, small, small, small nodes.
08:14I have made a general link list structure.
08:17I have made a cycle.
08:17It looks like this,
08:18which is cycle form.
08:19And this is my cycle.
08:21I mean,
08:22the distance is loop.
08:24Let's say,
08:24my distance is equal to D.
08:27What can I say?
08:29Whether it is D or P,
08:31Q,
08:32R,
08:33L,
08:33how much distance is this?
08:355 nodes,
08:3610 nodes,
08:3720 nodes.
08:38From 0,
08:40we will reach value to this value,
08:42like our slow and fast moves will increase.
08:46Why?
08:47Because slow is increasing.
08:48Slow is increasing.
08:49Fast is increasing.
08:51And the fast is increasing.
08:52And the two will increase.
08:53Because we have a loop.
08:55When we will not be able to move.
08:58So,
08:58the distance will increase.
09:00We will increase.
09:01We will increase.
09:01If any time it is,
09:02we will be equal to D.
09:03And the distance will be equal to D.
09:06That is equal to total distance.
09:11What can I say? If it meets something on this point, then if my slow pointer is here and the
09:17fast pointer is far away from it, it means far away from it.
09:20What does the distance mean far away from it? It means far away from it.
09:28It means far away from it. If it is far away from it, then in mathematical terms, my fast is
09:34also far away from it.
09:36We can measure the distance in two directions. If I am standing with my friends in a circle, then if
09:41I am standing here and my friend is standing with me,
09:44then I can see that if I am standing here, then the distance is zero.
09:48Or I can see that the distance is equal from the circle in the other direction.
09:53So the distance between the two directions is zero.
09:58And the distance between the two directions is equal to the circle.
10:01So what can I say? If it is moving, then the distance between the two directions is equal to one
10:07day.
10:08And the distance between the two directions is equal to one day,
10:10then my slow and my fast will be on both single nodes.
10:14This is mathematically always true in the case of a loop.
10:18So how do we detect the loop? We detect the loop by our slow-fast approach.
10:21So we can use the same way. In the beginning, I will initialize my slow pointer with my head.
10:29And my fast will also initialize my head.
10:34Then I have to run a loop. When will I run a loop?
10:50So the structure of the link-list assumed by the previous structure.
10:53I have to run a loop.
10:54I don't know that no cycle in last, there is no cycle.
10:55That is the condition.
10:57I always have to do that.
10:57On this side, I will increase the slow, first by one.
11:01And then the fast, I will increase the loop by two.
11:03But if my slow is equal to any point, what is the constant,
11:08what did I get to do on that point?
11:09Then I get to get the cycle.
11:11So I will directly return the true.
11:16True return means that the cycle exists here. If we believe that the cycle exists here and this is a
11:25normal link list in which the last name is null.
11:28So, one day fast will come here or here and we will go out of the loop.
11:33This loop means that this condition does not exist and this means that the cycle is not in my linked
11:40list.
11:41So, what will I do in the last name? In the last name, I will return false.
11:46False return means that the cycle exists in my linked list.
11:50We will write code in this way. Now let's talk about code.