Hello all and welcome back.
So in this video let us continue our discussion on linked list.
And now let us try to delete node in a linked list.
So let's get started.
Now the very first variation which we are going to cover is to delete the head node of our linked list.
So we have done the same when we were studying insert first the head node, then tail, and then any
node in our linked list.
Right.
So let us quickly draw a linked list.
So this can be my first element.
Let me just copy and make some more elements.
So yeah let's say we have some five elements.
They are joined like this.
Great.
So now if we think about how to delete in our linked list.
I think it's pretty easy, right?
So we just have to.
As of now this is going to be my head.
This particular node is my head.
We just have to.
Make sure that we move our head from this node to this node.
Right.
So this should let me draw it.
Nicely.
Yeah.
So this should be my updated head.
Which implies if I just do head is equals to.
Head dot next.
Then I should be getting my updated head for my linked list.
Right everyone.
So that's the basic idea behind making sure that we are able to get our updated head.
Now let us quickly compare this with array or a list.
So if we are given an array or a list, and let's say in that I have to delete the first element.
If I have to delete the first element in that, what happens is that each and every one has to shift
by one position, right?
Because we have a continuous stream of elements and we have a starting point, we cannot change our
starting point.
So in your arrays or a list, you cannot say that my list starts from L1, right?
Even if you take the slice of your list then it's actually create a new copy.
So that is also an operation which is heavy.
So I think all of us can understand this is going to be an own operation.
Whereas if I just compare it with the deleting node in a linked list, the head node time complexity.
Is going to be big O of one because it is a constant operation, right?
I am not doing anything, I don't.
It doesn't matter how long my linked list is, as long as I am just able to move my head, which is
pretty straightforward.
I can of course do the same thing, right?
So now let us see the same in code.
Every one once.
Okay.
So let's code it out in VS code.
Yeah.
Great.
So I have made a new file delete a node in linked list.
Okay.
In this we are taking the input.
So let us define a function delete at head.
We will be getting a head which will be the current head of my linked list.
And what I have to do is new head is equals to head dot next.
And I can just return my new head.
So let us try this out quickly.
I will just quickly tell the function again.
It's pretty straightforward.
We will be creating a new head which is going to be the head dot next.
Okay.
So if you go back to the diagram pretty easy.
I have I've deleted this connection, or rather I have updated my head is a better word.
No deletion of any connection.
Just updated my head that hey, now just point to the next node.
Okay, great.
So yeah, if I just do head here and let's say delete at head, I'm passing the head again and let us
print the linked list again.
Okay, great.
So I can just have print after deletion.
Okay great everyone.
So let us try to run this once.
Let us try to run this 1020 3040 minus one.
Okay.
So it is not.
Okay.
My bad.
We should have also updated our head.
Right.
We didn't catch it.
So this value didn't get changed.
So let us run this again.
So the error was that I just ran this right?
I have to catch my head again.
It is getting changed.
So let me just do that now.
Ten 2030, 40 minus one.
Yeah.
So we can see that it is getting changed.
The first node head ten.
Sorry ten is getting deleted.
Uh, one more thing everyone.
So whenever we are doing any update operations on our linked list, right.
Deletion, insertion, changing the value, then it makes a lot of sense to return the head and catch
it as well.
Whereas if you are just accessing, for example in print, we don't need to actually catch back the
head.
Rather it is not returning anything as well.
See, it is not returning anything.
So always remember this for any update operation, kind of any operation of kind where we are updating
something or deleting or inserting basically the update work.
Only then please make sure that you return the updated, uh, updated head and catch it as well.
Okay, great.
So yeah, this is one just one more case.
I would like all of you to think about.
What if our linked list is empty?
See, it gave an error.
Why did it give an error?
Because it gave an error.
Because we are trying to now access none dot next.
Which of course is a problem.
This is not.
This is something which we cannot do.
So one condition should be there.
If head is none, we should just return none as it is, which is my linked list is empty.
Okay, so always make sure that we are thinking about these cases.
Empty linked list.
Linked list with a single node.
Okay.
Whenever we are let's say doing something on the end such as head or tail, all these are the cases
which you have to think about.
And now if I run, this code is going to run perfectly well.
See?
Empty and empty okay.
so let us just check it on last, which is single node.
And I think it works fine.
Ten and after deletion no node is there.
Okay.
Because ten next will be null.
So when I make the new head it's coming out to be null only.
Okay so great everyone.
I hope that was clear.
Just let me quickly write for you all.
Uh, make sure to handle.
Edge case of empty linked list.
Okay, so again, when we do questions going forward, questions on leetcode or interview level questions,
we will have to make sure of these edge cases in them as well.
So that's why I'm putting in a habit that from the starting only when we are doing all this operation,
you are paying heed to these special cases.
Okay, so cool everyone, let's now meet in the next video.