• 15 hours ago
Transcript
00:00Besides remove first we can remove last
00:05Let's take an example
00:06If I have this link list
00:09There are 3 nodes
00:111, 2 and 3
00:13This is my next disapoint
00:14This is my next disapoint
00:16This is my next null
00:18Here is my head
00:20Here is my tail
00:22Now if I have to remove last
00:23I can say that I have to take the tail
00:26I have to take the tail from the first node
00:28And I will make the next node
00:31I will make the next node
00:32As I have made the next node
00:33And I have made the tail
00:35What will happen?
00:37This node will automatically delete
00:40So what steps will be
00:42The first step is
00:46If I call this node
00:47If I call this previous node
00:50Then my previous node
00:53Next
00:54Next
00:55This means
00:55This link will break
00:57And it will point
00:59The null value
01:01What will happen next?
01:03This will not completely delete
01:05Why not delete it?
01:06Because the tail is
01:07The last node is pointing
01:09So I have to change this tail
01:10So I will take a second step
01:12In which I will assign the previous value
01:16So I will remove my tail
01:18So I will remove my tail
01:19And now my tail will be
01:21My previous value
01:23Now all the values
01:25Are pointing here
01:26This node is alone
01:27In the memory
01:28As it is alone
01:29As it is alone
01:30The garbage collector
01:31will automatically delete it
01:34If we have to do C++
01:35Then I have to free it
01:38I have to free it
01:39But because C++ is not
01:41And in Java
01:42I have garbage collector
01:49So I will remove last
01:53Now I will create a new method
01:54To remove last
01:56Public
01:57Int
01:59Remove
02:00Last
02:02In this case
02:03There will be no parameter
02:03I will not have to
02:04In this case
02:05First of all
02:05The case is
02:06That if my link list is empty
02:08Then I know
02:09My size is 0
02:10In this case
02:11I will print simply
02:12Link list is empty
02:14And I will return
02:17Integer.min value
02:20Another case
02:21In any case
02:22If my size value is equal
02:23In this case
02:25I will delete my head
02:26And with my tail
02:28I will also delete my tail
02:28So here
02:30I will take
02:30Int value is equal
02:32My
02:32I can say
02:34Head value
02:35And tail value
02:36Both data
02:37will be the same
02:38I will create head
02:39And tail
02:40I will create
02:41Null
02:41Here I will do
02:43Size
02:430
02:43And here I will return
02:45My value
02:47Now what do I have to do
02:48In the rest of the cases
02:49What do I have to do
02:50In the rest of the cases
02:51In the rest of the cases
02:51I will traverse
02:52I have to reach my previous node
02:55To reach my previous node
02:55What do I have to say
02:57And I can see
02:57Your previous node
02:58Which goes to its index
03:00I will acknowledge
03:03The value of index
03:03Size
03:03or Size
03:04as I will see
03:05I would search the mini
03:20For мен
03:21int i equals 0 i less than size minus 2 i plus plus and what will I change in this
03:28loop?
03:30I will change my temporary node. I will initialize it with head or I can say previous node.
03:38I will initialize it with head. Every time previous is equal to previous. Next.
03:46When this loop is finished, then I will stand on actual size minus 2th index.
03:51What will I do there? First, I will assign previous node to the next node.
03:58I will assign the previous node to the tail. I will assign the size to the previous node.
04:05Before I delete the value, I will store it. Value is equal to previous node to the next node.
04:13I will add the data to the tail. I will return my value to the next node.
04:21I have used this 4 steps. I have deleted the last node.
04:27What did I do? I store the data. I need to delete it.
04:31How did I store it? The previous node to the next node.
04:35I can write the tail.data as well. I will point the next node to the next node.
04:41In the third step, I assigned the tail to the previous value.
04:45Then I have done the size minus minus and returned the deleted value.
04:50Let's do this. Save.
04:51Now, what will I do in my link list?
04:53In my link list, I removed first.
04:56Now, I am going to do the link list.remove last.
05:00I will print the link list here.
05:03And after that, I will print the final size of my link list.
05:08Let's do this. Save and now run.
05:12In the beginning, I had a link list. 1, 2, 3, 4, 5. Then, null.
05:15When I removed first, I deleted my 1.
05:17I deleted my 1.
05:19Then I removed last.
05:20Then I deleted my 5.
05:22So, only 2, 3, 4.
05:24Now, when I printed it in the size,
05:25I have 3 nodes in the link list.
05:27We don't count as a node.
05:29It is only 2, 3, 4.
05:31So, the size of the link list is printed here.
05:33Which is equal to 3.
05:35In this way, we can also remove first.
05:37We can also remove last.
05:38We can also remove last in the link list.