Hello all and welcome back.
So in this video now let us do the last variation of deleting a node in my linked list.
That is I want to now delete the node by value okay.
So let's get started.
First let me draw a linked list.
So one two.
Three.
Second four.
And say five okay.
And we can just take the last one as none.
None okay.
We all know this is the head.
Now I am drawing it like this.
Because when you will be doing question since you have got the idea now this is going to be lot easy,
right?
It has the data and the pointer.
So just a small way of drawing it.
But uh, if you have any issues like I don't think it should be there, but yeah, if it is then please
draw the boxes and make sure that you understand it properly.
Okay, great.
Now, in the head part, let us just think out loud and see what sorry in the value part.
Let us see what can be different cases.
Right.
So first is that again you are asking me to delete the head only head value.
Okay.
So that is the case.
And again in this question we are deleting the first occurrence.
So even if it is let us say 444.
I am deleting the first four right.
Great.
So second thing you are sorry value is not present.
Okay.
Let us write it in short value not present okay.
And again these are very important to always write and even confirm with the interviewer.
So that is why I am writing them and making you go through the way it will be solved in an interview.
Third value is present, right?
So I think these are the major cases which we will get.
You are asking me to delete the head then value is not present or value is present right now, just
like we did in the delete at an index part, and rather and in delete at tail as well.
You will see one thing that if I want to let's say delete this for okay, this is my overall say element
which I want to delete.
Okay.
Delete for then understanding is this.
We will have to stop one step or other one node before right.
So we can change the connection.
Again the understanding is very very similar.
Okay.
We just have to make sure that we stop a node before.
because if you are able to stop a node before then, you just have to make sure that you make this connection.
Nothing else, right?
This is just the connection which you have to make and you will be good.
And I think this we have done now lots of time.
We have to first make sure that this is the first connection which we break.
And then this should be the second connection which we make.
Right?
So instead of missing out on all the linked list one second.
Yeah, this is the first one which we make.
And this is going to be the second one.
Right.
So actually it will be a lot similar to the one which we did in index.
So let us now do it everyone I hope the overall understanding is clear.
I will start a loop.
Make sure that when I get the value, I am checking that value on the next, not on the current.
Because again, if I have to delete for and I stand on for, then I cannot delete this, then I cannot
delete this.
And again, we are not changing the values or the data in our linked list.
Right.
So you cannot say that you are allowed to change the data.
That is some condition which we have not taken.
So you will have to go to three.
Right.
And then ask three to that.
Hey, please forget four and move on to the next element which the four was remembering, so that this
four gets eliminated from my list.
Okay, so that's the basic idea everyone.
I hope it is clear a lot we have done this many times.
Again, at this point I will also now ask you because you have done so many questions.
Pause the video and try to do it yourself.
If not, then of course we will be doing it as well.
Great everyone!
So I hope this was clear.
Now let's go and code the same.
So def delete one second.
Delete a node at a by value.
Right.
By value.
Great.
So we will be given a head and a value.
Okay.
So pay attention, everyone.
Let me just clear it.
Close it so we can focus on this.
Uh, I think zoom should be fine.
I can write it like that as well.
If, again, your head is none, you cannot do anything.
If your head is none, just return none.
You can say list is empty or something like that, but yeah, we can just actually print it because
it is in iteration print list empty to let the person know that maybe we are doing something wrong,
right?
Then next part if head dot data and is equals to value right?
So you are giving me one here.
In this particular case, you are asking me to delete one which is the value of my head.
So I have to now in this particular case always connect when check with data.
We have not done this ever before, but yes, this is what we have to do.
Okay, great.
If this is the case, we can just return head dot next.
Right.
So again I will write this boundary case when head is value.
And again this will be a lot helpful when we write the recursive way of doing this.
Right.
Right now we are going to make a temp which is going to be head.
This time we don't need a count because we are working on value this time and not on index.
I have to now write my loop.
Okay, so while this time now pay attention everyone.
So one more thing you have to understand when I'm going to use my temporary variable okay?
I have to make sure that the next I am comparing.
Because always I have to compare the value of next right?
And I have to make sure that I'm able to reach to the last node somehow.
So we have to make a check that temp dot next dot data.
If we do temp dot data, it's going to be a problem.
The reason let's say you use temp dot data, then you will come on for it will be the value.
But again then you cannot do anything.
You cannot delete it.
As of now we are not maintaining any previous point or anything.
Once you come on this, for there is no way you can go back.
There is no way you can go back, right everyone?
So I hope this is clear a lot.
So again we have handled.
Let me mark that we have handled this case okay.
Next.
Again what we have to do while temp dot next is not none okay.
And temp dot next dot data okay is not equals to value.
If both of these cases are true I should move my temp.
So all I'm saying is that hey till the next node is not none and the data is not equal to the value
I'm looking for, please move the pointer How that will actually happen.
So it will be something like this.
Pay attention everyone.
123.
Four five.
Temp will start from here.
Temp.
I'm saying that hey, is the next node.
None.
It will say no.
Okay, great.
Is the data equal to four?
It will say no okay then move forward.
So temp will come here.
Now again the same check is the next data.
Next of temp none.
You will say no.
Is the data equal to value?
I am looking forward.
No.
Then move next.
Okay then next check is the temp.
None?
No.
Is the data equal to value which I am looking forward to?
Yes.
So break the loop and you will have the in temp.
You will have three this time because you have three now.
You can work on making sure that you make this connection directly.
Okay great.
So let us now one second.
Yeah.
Let us now try to code this as well everyone.
So this is done now before you are able to.
So what we have to do now temp dot next I can say is equals to temp dot next dot next.
Right.
So if I'm able to make this change just so for next again this is very much same as we did here.
Note to be deleted temp dot next node after deleting node.
So maybe let me copy this okay.
If we are more comfortable in this approach we can do this as well.
Let us do this right.
So I'm finding node to be deleted which is my fourth node after deleting node which is my five and then
temp dot.
Next I'm just making it node after deleted node and now I can return my head.
Okay, so totally works fine.
Let us just quickly try it.
I think we have.
We might have something which we are missing, but let us see that.
No worries.
Let us first try this.
Uh, one second, I think I've not called this.
So delete node by value.
Okay.
Um, n minus one.
Great.
So let us clear this.
And this again.
Uh, 12345 minus one.
Okay.
I think we have made a mistake.
So we are deleting three instead of four, but I think should be fine.
Right.
We are passing this value as three.
I forgot that we are passing the value.
But I think this will not change anything in the question.
So see, node three has been deleted, right?
Great.
Very good.
Let us check with empty list.
Seems to work fine.
List empty.
Great.
One.
Two.
Three.
Four.
Uh.
My bad.
Okay, we cannot do it.
So something which I want to try is everyone.
Let's say ten, 20, 30, 40 minus one.
See?
Now I'm getting an error.
Now why am I getting an error?
Can anyone think on that?
We have done this lots of time.
The error lies because this loop, this whole loop, it would have ran, but it won't have found anything.
So my temp dot next would have been none.
We would be on tail node.
And because I'm tail node.
So see, this condition is the one which is making me stop temp dot next is none.
So it stopped right.
And now because my temp dot next is none, that implies I am on tail node.
So how can I access the dot next of this again?
So one condition to handle this all.
If temp dot next is none.
That in a way means that we have actually gone through each and every node and we have not found the
value because finally my temp dot next is tail.
So if I start it from head and I have reached tail and I have still not find your value, then that
means the value is not there.
So print value not present.
Return head as it is.
And now your code is going to work fabulously well.
Let us run this and clear it ten 2030 4050 minus one C value not present and after deletion same linkedlist
we are getting right everyone.
So I hope this is a lot clear as well.
Now this whole way of handling all these things, we have done this a lot of time.
So yes, we have handled value not present and value present both of them as well.
So let us mark them right.
Okay.
Great.
So yeah I think this was a very much on the way we have done all these things as of now.
All the questions now just one thing more everyone.
So you have to just try to do this recursively as well.