Hi everyone.
So we have successfully coded the solution.
Now let's walk through the code and again understand what's happening in each stages of the code.
And again this is important because in any interview you will be asked to walk through the code.
Once you have written.
Now this part of the code and this part and this part of the code is just written so that we can test
out the function once we have written it.
All right.
So this part of the code creates this doubly linked list.
Right.
Which is one pointing to two pointing to three to 4 to 5.
And these two point to null.
So the head is one and the tail is this one over here which is five.
Now again quickly looking at the code.
So we have a class node.
With this class node we have created these five nodes 12345.
Right.
And then we are using the function link nodes where we pass two nodes to link them.
Right.
So node one dot next is set as node two and node two dot prev is set as node one.
So using this we are uh and then here we are calling this function to connect these nodes.
And then we have linked list.
Doubly new is a new doubly linked list.
And we have our doubly linked list class over here.
So we are creating linked list doubly over here.
And we are setting its head to one and tail to five.
So this part of the code is used to create this doubly linked list.
Now we have written the instance method remove node right.
And the input to this function is a node.
And we are calling it and we are passing five over here the node five.
Now we are inside this method over here.
All right.
Now let's see the case where we are passing node five.
Over here we can see that this is the tail right.
So we find that this is the tail.
And because we check whether this dot tail is the node.
And it turns out true in this case because we are passing five over here, which is the tail.
Now we are setting the tail the new tail as node dot pref.
And this is the node right.
This is the node.
Let me just write that over here.
This is the node.
So no dot prev is this one.
So this is node dot prev.
And we are making this the new tail over here.
Right.
So over here then we are checking whether Node.prev is there.
Yes it is there.
Which is this node over here.
Node four.
All right.
So we are setting node dot dot next.
That is this connection over here.
We are changing this connection over here to node dot next right.
So node dot next over here is null.
So this is node dot next.
So we are changing this connection and making it point to over here.
So that's done over here.
Right.
And then we are checking whether node dot next exists.
This is node and node dot next is null.
So this is falsy.
So we don't go inside over here.
And finally over here we said node dot next to null and node dot prev to null.
Right.
So we are removing node dot.
Next is already null and node.prev.
This connection is removed and it is set to null.
And we are done with the solution right.
So at the end of this we will have one and we have two.
Then we have three and we have four.
And this over here points to null.
Right.
This also points to null, and in the case of node five, it points to null in both the sites.
So we have removed node five from the input doubly linked list and we have successfully done that.
Now let's take a look where the input is the head.
Now let's say over here we have dot remove and one we are passing the head.
So over here we we would have identified it as the head.
And we set the node dot next.
So in this case this is node.
So node dot next is set as the new head right.
This dot head is node dot next.
So this is the new head.
Then over here we will check whether node.prev is there.
So node dot prev is not there.
So we don't come inside this if loop this this becomes falsy.
Then we check if node dot next is there which is true.
It's truthy right.
It's there.
So we say not dot next dot prev right.
So this this connection over here.
This connection over here is set node dot.
Next.
This is node dot next it's prev right.
It's prev is set to node dot prev over here.
And then finally over here.
In this part we disconnect this connection right.
So node dot next is set to null and node dot previous set to null.
So at the end of this we have two.
And that's pointing to three which is pointing to four which is pointing to five.
This is pointing to null.
This is also pointing to null.
And in the case of node one both the sides point to null.
So that's what's happening when we pass in the head.
And let's say we pass the node three.
In this case what happens.
We identify that it's not the head and it's not the tail right.
So we come over here we see that Node.prev is there which is this one.
This is Node.prev.
This is node and this is node next.
Right.
So node dot prev dot next.
So this connection over here is set right to node dot next which is this one over here.
And we see that node dot next which is this over here exists.
So no dot next dot prev that is this connection over here.
This connection over here is set right.
We are changing this connection from here to here.
And finally over here what do we do.
We set node dot next and node.prev to null.
So we remove these two connections.
And we will get one that will point to two which will point to four.
And this will point to five.
And these two sides point to null.
And then in the case of node three both the sides point to null.
And yes, we have successfully coded this.
And we have seen that this solution over here is O of one space and time.
We are not using any extra space, and we are not traversing the doubly linked list.
Right.
The node over here we can see is given as input.
So we don't need to traverse it.
Therefore this operates in constant time as well.