• 15 hours ago
Transcript
00:00Next time we are going to talk about an important operation which is a remove operation in link list.
00:04If I have to delete a node in the link list then there are two ways.
00:09The first thing is that I will delete that node first.
00:13If I have removed first then my head node will delete.
00:17The second way is that I will remove last.
00:19That means my tail node will delete.
00:22First let's talk about how to do head first.
00:25I have a link list in which there are three nodes.
00:32Here is value 1, here is value 2, here is value 3.
00:36This is my next node.
00:37This is my next node.
00:39And this is my next node.
00:42Here is my head.
00:44If I have removed first call then I can simply say that my first node will delete.
00:51As this node is deleted then I will make my second node.
00:56So what will I do?
00:57I will remove this head and I will remove it here.
01:01So the next time when someone tries to use a link list,
01:05the link list always starts from the head.
01:08So the link list starts from here.
01:10So this node will automatically delete my garbage collector.
01:15Why will the garbage collector delete it?
01:18Because it will know that it will detect that there is no head here.
01:22We cannot reach this node until this node.
01:24Why cannot reach this node?
01:25Because there is no other node that will return to this node.
01:29We are always talking about the link list.
01:30We are always talking about the link forward.
01:33We call it singly link list too.
01:36Singly link list is the link list that always happens.
01:42So this link is not the other node that points it towards this node.
01:46There is another node that points it towards this node.
01:48There is another node that points it towards this node.
01:49But I also have made it as a head.
01:52So what can I say?
01:53There is no way to reach this node.
01:55There is no way to reach this node.
01:56So what will happen?
01:58This node will automatically delete.
02:01So what is the way to remove first?
02:02What is the way to remove first?
02:04What is the way to remove first?
02:04This is the way to change my head's value.
02:07I will change my head's value.
02:09This is a single line.
02:11Which constitutes my remove first.
02:14Now let's write a code for this.
02:16I will create a method in my link list.
02:19I will name public void.
02:23In fact, I will return it as int type.
02:26What will I return?
02:27I will delete the node.
02:29I will return its value.
02:31Public int remove first.
02:33In remove first,
02:35I will not have any parameter.
02:37Because my head already has.
02:39I will delete the node.
02:40That is the data head.
02:42What will I do?
02:43I will do head next.
02:46And I will return its value here.
02:50So in this way,
02:51I will perform remove first operation.
02:53How was my link list in the beginning?
02:55In the beginning, the link list was 1, 2, 3, 4, 5.
02:57Now what will I do?
02:59I will call for the link list.
03:02Remove first.
03:04And after that, I will print my link list.
03:09Let's save and run.
03:14So what was the first one?
03:15What was the first one?
03:151, 2, 3, 4, 5.
03:16So what was the first one?
03:17As I have removed first,
03:171 is removed from my link list.
03:20And I have left only 2, 3, 4, 5.
03:22Now what do I have to do in remove first?
03:23What do I have to do in remove first?
03:24I have to look at some special cases.
03:26The first one is the first case.
03:27If I have removed first,
03:30I have removed first.
03:30But here, my size is equal to 0.
03:33Size is equal to 0 means that my link list is empty.
03:36In that case,
03:36I will not delete anything.
03:38Because there is no node in the link list.
03:40So in that case, I will print.
03:42Link list is empty.
03:44Link list is empty.
03:46I will return infinity.
03:48I will return integer.max value.
03:51Or I can also do min value.
03:53I have returned minus infinity.
03:56I have not returned any valid value.
03:58Besides, there will be another special case.
04:00This will be a special case.
04:02If my link list size is equal to 1.
04:06In this point, my head is also the same.
04:08And my tail is the same.
04:09So in that case,
04:10I have to change my head and my tail.
04:13That's why I need to make this special case.
04:16So what will I do here?
04:18I will do my value as my head data.
04:22And head as well as tail.
04:25I will create null.
04:26I have to create null.
04:28If I have to delete this head data.
04:30And I will return this value.
04:33So I have to make two special cases.
04:34I have to remove first.
04:35One is my size is equal to 0.
04:38Which I have left the link list.
04:39The other is my size is equal to 1.
04:41Which I will create null.
04:42And my tail will be null.
04:44I can handle all the other cases.
04:45I can handle it using these lines.
04:48Let's do this.
04:49Let's do this.
04:49Now what can I say with this?
04:51I can say that if I have to track my size.
04:52I mean if I have to track my size in this function.
04:56Then I should remove first.
04:57I should also change my size.
04:59That answer will be absolutely.
05:01Remove first.
05:02I should also change my size.
05:04Because my link list should know.
05:06What is the size in current state.
05:09So when the size is equal to 0.
05:11Then I will not change.
05:11When the size is equal to 1.
05:13And as I have made the head and tail null.
05:15What is my size?
05:16Now I have to change my size from 1.
05:17It is equal to 0.
05:19And in the rest of the cases.
05:20In the rest of the cases.
05:21In the rest of the cases.
05:22In the rest of the cases.
05:22I will value my size.
05:23Size minus minus.
05:24So every time.
05:25Remove first.
05:26In the special case.
05:27Size 1 was 0.
05:28It is equal to 0.
05:29But in the rest of the cases.
05:30I will place my size-