Hi everyone.
So we have successfully coded this solution.
Now let's quickly walk through it.
Now this part over here of the code was taking care of a situation where, for example, the doubly
linked list is just one node, like for example, this one over here, and the node position and the
node insert is both one itself, that particular node itself.
Right.
So if node insert is the head and the tail right.
So if that is the case then we just need to return.
We just don't need to do anything.
So we just take care of this edge case over here.
Now we proceed.
Now first we go ahead with the case where we pass the node insert to the remove function which we've
already written, and we take it out of the doubly linked list.
So if it's part of the doubly linked list, this will ensure that the node is no longer part of the
doubly linked list.
So that's what we're doing over here.
Now for example, if this was the doubly linked list and let's say we are passing the position node
as node two and the node two insert as three.
In this case, after this line over here, what will happen is we'll have the doubly linked list look
like this.
That is three is taken out of it.
And then we have three over here.
And it's pointing to null on both the sides like previous and next.
Now again two is the position node and three is the node insert over here in this example.
Now we proceed over here node insert dot prev and node insert dot.
Next that is this is the uh node.
Insert this one over here three over here.
Now it's previous.
And next.
That's what we're setting over here.
Its previous is set to node position dot prev which is this one over here.
And its next is set to node.
Position this one over here.
So we are making these two connections over here.
All right.
So that's done.
Then in this case we are checking if node position is the head.
That is are we inserting before the head.
Now in this particular example that's not the case.
So we come to the else part and we do node position dot prev which is one dot.
Next.
That is this connection over here we are breaking this connection and setting it to node insert which
is this one over here.
Now finally we just need to remove this connection and make it point to node insert.
This is node insert right.
So we are doing that over here.
We are doing node position dot prev which is this.
This one over here.
And dot prev this this connection over here is broken.
And that is set to node insert which is this one over here.
And there we have our solution.
And in case if it's if it was the head, if we were inserting before the head, we would have set the
node that we are inserting as the new head.
And then we wouldn't do this part over here because there would nothing be there.
Right.
So this this thing over here would not be done.
Right.
We'll just set the new in the node that we are inserting before the existing head as the new head.
And this is the solution that we have done.
And as we have seen this runs in constant space and time.
You can see we are not using any additional space.
So constant O of one space and we are not traversing the doubly linked list.
Right?
The node and the position node both are given as inputs.
We are not traversing the doubly linked list.
That's why it's running in constant time as well.