Hello all and welcome back.
So in this video let us continue our discussion on linked list.
And now let us try to delete the tail node in our linked list.
So let's get started.
Now I have drawn a linked list which is in front of you again.
We have seen this many times in this linked list.
This is my head and 40 is my tail right?
So 40 is the tail.
And ten is the head.
Now our whole aim is to delete 40.
Okay.
And one more thing everyone.
So all these operations we are doing because these are the very basic operation which we will be, which
we can do in a linked list.
Right along with that, as I have told multiple times going forward, the understanding which we developed
by doing all these operations, that is what is going to help us in solving those questions, right?
If you are able to just do all these questions, believe me, all these operations, 80% of questions
you will be easily able to do.
Okay.
good.
So now if we see if I have to delete this particular node.
Okay.
Then let us think how exactly can we go about it.
Now what I feel is that if we want to delete this node, the node 40, just like we did in insert,
we should have some pointer through which we are able to reach at this position.
So our temporary pointer which we are going to make it should be able to reach this position.
The reason for that is that if I reach on tail node, then how will I delete the same from my linked
list?
I will repeat it again when you have to insert something at tail.
Okay, insert something at tail.
We need to change the connection.
This connection.
Right.
But when you have to delete something okay.
And this is going to help you a lot in future as well.
The understanding of this thing, if you want to delete something then we have to change the connection
which is before that node.
So I think this is a very important learning.
So we will write that as well when we want to delete a node.
We stop.
At a node before right.
Similarly when we have to insert a node.
We stop on the node.
Where.
We will insert.
Okay.
So again everyone this is very important points.
I will again help you to understand this when I have to delete a node.
So if this is the node I have to delete I have to make sure that I stop here because this is the connection
which joins this particular node to my linked list.
On the other hand, if I have to insert a node, let's say at tail or at this position, then I have
to make sure that I stop here, or if I have to do a tail, then I have to make sure that I stop here.
Okay.
Let us discuss for tail only for the time being, when I have to insert for tail, I have to make sure
that I stop on tail node.
But on the other hand, for deleting, I have to make sure that I stop at node which is one before the
tail.
Okay, so I hope this is clear.
Everyone again going to help you a lot.
So now let us understand what is the overall property which this node has okay.
Now As like ever linked list node has two properties data and the next data can be anything.
Of course we don't have to worry on that.
The next property I think, which we can use is while.
Temp dot next right dot next is not none.
So this I think is the condition we can use.
Because if I have to stop at this node then I have to add some condition.
The condition should be that my next node is a tail node, right.
To check if my next node is a tail node, what we have to do we have to just check the next of next
right.
So yeah, this is something which we are going to write our logic on everyone.
So pay attention.
So let us just write a function here.
Def delete at tail Head.
Now, as always, let us first handle the base case.
Okay.
So what is going to be the base case?
The first one I think is if our head is not set forward, if our head is none, what was the other base
case, which I always ask you to handle is when we have a single element.
Now, when we have a single element in that case also we will delete that node, right?
In case when I have just a single element, I will delete the node.
When I have a single element.
What does that mean.
That effectively means that so or head dot next is none right.
So this is what a single element tells me right.
So for example if this is a linked list.
Then this will be none.
Now because this is none.
And let's say this is my head and this is only my tail.
I want to delete the tail.
Right?
So you will be deleting the head as well.
The check is if my head dot next is none.
Right.
So yeah, I think that can be used.
We can just return none because it is my linked list which is empty if the list is empty or has one
node.
Right.
Great.
So in a way you will see that to check the tail node, this is actually the check head dot.
Next is none okay.
So I can actually create another function diff is tail node.
In that I can get a node.
I can maybe let's say get a node.
And how can I check if it's a tail node.
If my node is let's node dot next.
If it's none then I can just simply return true right?
I can say that.
So again this can be another function which you can ask to.
Right.
And if node is none then I can simply say that.
in a similar manner.
We can just return true here as well because again, it's a null node.
In all other cases I can return false, right?
So this I'm just telling you that to check again, if you go back to the diagram and see a tail node
is the one whose next is null.
We have seen this multiple times right.
Great.
So now let us write the code for this.
We have handled the cases the edge cases.
Now I have to just make a temp which is head.
Okay.
Now till when I have to run while temp dot.
Next dot.
Next.
Okay.
So either you can do it like this or we can now use this function as well.
Is tail node okay.
So maybe I can pass temp dot next here.
So all I'm doing is I am asking that hey stop me a step before when there is a tail node next to me.
So that is what I'm doing.
Temp dot next.
This is a tail node okay.
Stop me.
So this is something which we are doing.
So while temp dot next is not none.
We just have to keep on increasing our temp.
Great.
Now once I have done this, once I have done this, I will be coming to this particular node.
And if we are on this node to just delete this last node, I just have to change the next pointer and
make it null.
Right.
So this is the property which we have to use.
So now just this will become none.
And let's delete this connection right everyone.
So pretty straightforward.
Just we have to understand that what exactly in deletion we have to do.
So temp dot next equals none.
And we can just return our head.
Pretty straightforward I believe we have just understood how to handle the deletion case.
That is to stop a step before.
Great I think.
Yeah this will work fine.
Let me run this quickly here.
So ten, 20, 30, 40 minus one seems to work fine.
Ten.
Oh my bad.
Minus one seems to work fine.
So for an empty linked list as well, it's working fine.
Ten minus one for a single node as well.
It is working fine.
Okay, everyone.
So I hope the overall understanding is clear.
Major case is just that only.
So if it is ten, 20, 30, 40 and minus one.
I am stopping at 30 here because of this, this whole code.
Okay, I can just do one thing as well to show you print temp dot data.
Okay.
Data of node.
We have stopped it.
So let us run this.
So it's a lot more clear 1020 3040 minus one C date.
Of note we have stopped is 30.
Ten, 2030, 40 and we have finally just deleted our node.
Right everyone?
So please make sure that again, I'm helping you to visualize all this stuff so that you remember and
understand.
Uh, if you require you can just run this on Python Tutor as well, though I think it should be a lot
clearer with this diagram as well.
Okay.
Uh, will suggest that always practice with diagram as well.
Uh, in interviews or any way we might not have Python tutor.
Right.
So understand but then try to draw the diagram for that as well.
So that's why I'm using both the, uh, both the overall techniques so that you understand and remember
everything.
Okay.
Cool.
Then everyone uh, now in the next assignment, which you will get after this, you have to try and
do this question recursively.
So we have done the same thing by iteration.
You have to now try and approach the same question by doing uh by using recursion.
So yeah, please try to uh, just do it once.
If you get stuck there will be hints and a solution video, which you can refer to.
Thank you.