Welcome back.
Let's try to think through how we can solve this problem.
Let's say the given doubly linked list is this one over here.
Now there are three possibilities, right?
One possibility is that the input node which is to be removed is the head.
So that's a possibility right.
So we are asked to remove the head in this case.
Now another possibility is that the input node which is to be removed is the tail which is this one
over here.
Right.
So that is case two.
And the other possibility is that we are asked to remove a node which is not the head nor the tail.
Right.
It could be one of these two nodes.
Now these are the three scenarios.
Now let's try to solve this question with the perspective of these three possibilities.
Now if the input node is the head we are dealing with case one right.
In this case, what is it that we have to do.
We have to find the next node right.
Definitely.
We have to find the new head.
Right.
So we are trying to remove the existing head.
So the new head will be the next node which is this one over here.
And we have to set it to the head.
Right.
So this is something that we have to definitely do.
Now what about the case where we are asked to remove the tail.
Right.
So if we have to do that that's case two which we have written over here.
In this case we have to find the previous node to the existing tail and make that one the new tail.
Right.
So this deals with the case where we are trying to remove the head and we are trying to remove the tail.
So these two things that is setting the new head or setting the new tail has to be done only in these
two respective cases.
Now let's look at a generic case.
So we have four nodes over here.
Now let's say we are trying to remove this node over here.
Now what do we do.
We check if there is a previous node.
All right.
So if there is a node over here that means if the previous node exists.
And if it exists, then we have to make its next to point to this node over here.
Right?
We have to make its next point to the next node, which comes after we have removed this one because
this node is no longer there.
Right.
So this one has to point to this one over here.
Now remember we are we have to make this check over here.
If previous is there.
Why is that.
So let's say we are trying to remove the head.
Now there is no previous over here right.
So if we don't make this check and if we just say that no dot previous dot next is this node over here,
then we will be actually writing null dot next.
Right.
Because if we are trying to remove the head, the previous value over here is null.
And then this operation would be null dot.
Next.
Right now this is something which will throw an error and we don't want this.
That's why we have to check if the previous node exists.
That is if it's not null.
And if that is the case that means there is a node over here.
There is a node which is previous to the node that we are trying to remove.
Then that node has to be made to point to node dot.
Next right.
This is node dot.
Next.
Let me just write it over here.
This will be node dot.
Next if this is node this is node dot dot dot next right.
And this will be node dot prev.
Right.
So no dot pref dot next has to be made to point to node dot next.
And again.
Similarly if the next node exists that is if there is a node over here then what do we need to do.
We need to make it to point to this node over here because we are removing this.
Right.
So basically you have to remove this connection and make it to point to this node over here.
Similarly when we did node dot dot next is equal to node dot.
Next we were removing this connection and creating this connection.
Again the same logic appears over here.
Let's say we are trying to remove the remove the tail.
In that case there is no node dot next right.
So if you do node dot next dot prev is equal to node dot prev.
In that case we would be saying null dot prev and that will throw an error.
We want to avoid that.
That's why we need to check if there is a next node.
If there is a node after the node that we are trying to remove.
In that case, we have to make that particular node's previous value.
The previous value to the node.
Right.
Again I'm I'm using previous and next over here.
It's important to visualize it so that you're very clear.
So if there is a node which is next to the node that we're trying to remove, that means if this node
over here is there, then the previous node to this node has to be made.
This node over here, which is nothing but node dot prep.
So this is what we need to check.
Right.
So this was step one where we checked whether we are trying to remove the head or the tail.
And in that case we had to reset the head or the tail.
Now in the generic case we are just checking if there is a previous node.
If it's there, we are creating this connection, removing this connection and creating this connection
over here.
And if there is a next node, we are removing this connection and creating this connection over here.
Now as you can see, we are almost done.
Let me just make some space over here.
There is just one more thing that's remaining.
You can see that this node, which is the one that we are trying to remove, still points to these two
nodes over here.
Right?
So we want to remove these two connections as well.
So let me just draw that over here.
So we have this node over here which is I'm just drawing it over here.
Now currently it's pointing to these two nodes.
Right.
So we have to remove these two connections.
And we have to make it to point to null.
So these are the three steps.
All right.
So let's quickly reiterate.
First we check whether we are trying to.
Remove the head or the tail, and in either case we have to reset the head or the tail appropriately.
Second, we check if there is a previous node.
If it's there, we change the connection like this.
And if there is a next node, we change the connection like this.
Right?
Again remember the important thing is to visualize this.
If you can visualize this accurately then to code this becomes very easy.
And finally we need to remove these connections because even after creating these two connections over
here, still the node which is to be removed points to these two nodes, right?
Which was the previous case.
So we want to remove that.
So we need to set the nodes.
This is the node that we want to remove its previous and its next have to be set to null.
And we are done.
So that's the solution to this question.
Now let's quickly look at the complexity analysis of this question of this solution which we have come
up with time and space complexity of this solution is O of one or constant time and constant space.
That's because we are not traversing the doubly linked list, right?
The node is given as input to the function.
And again we are not using any extra space.
So the complexity of this solution is O of one time and space.