Hi everyone, I hope you have given it a thought about how you could implement the solution, which
we discussed in the previous video, to write a function which will remove a node when the node is given
as an input right and that node is part of a doubly linked list.
Now before we go ahead and write the instance method.
So I have just written some code over here so that once we are done writing the instance method, we
will be able to test our code.
So let me just quickly walk you through what I've written over here.
Now we have a class node okay.
And when we call this class node we will be passing a value so that we can create a node.
Right.
So using this class node I have created these nodes over here.
So node 1234 and five.
They have the values 12345 respectively.
All right.
So and then I have a function over here.
Now when I pass two nodes to this function I will connect them together.
Right.
So node one dot next is set to node two.
And node two dot previous is set to node one.
So that's what's done over here.
So that's just to reduce the number of lines of code.
We could do it manually also.
Right.
We could do it with multiple lines of code also to connect these nodes together.
And I have a class doubly linked list over here.
Now over here I don't have any input.
And this dot head is set to null.
And this dot tail is set to null.
And using this class doubly linked list I have created linked list doubly over here.
Right.
And initially the head is null and the tail is null of linked list doubly right.
This is a doubly linked list.
And then we connect the nodes 1234, five using the function that we have written over here.
And finally we set the head of linked list W21 and the tail of linked list W25.
Now before we write anything.
So just we have we have not written a function yet.
Let's see how this looks.
So let me just run this code.
All right, so I'm just running it.
And let's take a look at linked list doubly.
All right.
So over here you can see that.
Let me just make this bigger.
You can see that the head is having is a node with value one.
And the tail is a node with value five.
So one is pointing to a node with value two.
And the previous node of one is null.
So this is a valid doubly linked list.
Now the node with value two is pointing to the node with value three, and the previous node of the
node with value two is the node with value one.
So in this way you can just open over here and see that we have already created a doubly linked list.
So it's called linked list doubly over here.
All right.
Now let me minimize this.
And let's go ahead and solve the question which we discussed.
All right.
Now we are going to write an instance method.
So let's call it remove and it will be written over here right.
And we will be passing a particular node to this function, right.
The node which we want to remove.
Now how do we go ahead with this code.
Remember we have discussed that there are three cases.
The first case is that if the node to be removed is the head.
The second case, as per the previous video where we discussed the method, is the case where the node
to be removed is the tail.
And then we check whether the node to be removed has a previous value and we do something.
We check if the node the given node has a next value.
We do something, and finally we set the previous and next pointers of the given node to null.
So this is the process that we are going to follow right?
So try to visualize it based on the previous video that we discussed.
Right.
So if you can picturize this then it's very easy to code because you are very clear on what you want
to actually achieve.
All right.
So first let's check whether the node to be removed is the head or not.
So let's check that over here.
If this dot head is equal to the given node.
If this is true then we need to set the head as the next node right.
So this dot head has to be set to node dot next.
All right.
Now, what if the node to be removed is the tail?
Let's check that if.
This dot tail is equal to the node to be removed.
In this case, we have to set the new tail to the node which is previous to the given node, right.
So node dot prev.
So again remember these are this next and prev is defined over here.
Right.
So you have to use the same.
So let's say you're writing dot val over here and somewhere else.
You are writing this dot value.
You will get an error.
So you have to use the same thing.
So over here we are writing this dot a list node dot pref.
All right.
So we have taken care of these two scenarios.
Now what's the generic scenario.
We are going to check if the node which is given has a previous value.
Let's check that if the node given has a previous value then what do we do.
The previous value.
Next value has to be set to the node's next value, right?
Just try to picturise it.
And again remember we have discussed this in the previous video.
Let me just quickly show that to you over here.
So let's say we have one pointing to two.
Again it's doubly linked list.
I'm just drawing a line indicating that it's, uh, pointing in both the directions because I don't
want to increase, uh, much what I'm writing over here.
So one then we have two, then we have three, then we have four, then we have five.
All right.
Now let's say we want to remove node three.
Now we are checking whether there is something at the position node dot pref.
So that's this node over here.
Right.
So if it is there this one has to be connected to the value of to the node over here.
Right which is node dot next.
So that's what we are trying to do over here.
If node.prev is there if it's truthy that means if it's not null or if it's there then node dot pref.
Dot next.
Right.
So by doing no dot prev we get to here.
Remember we are taking the example of removing node with value three.
So no dot prev gives us this node over here.
Dot next gives us this one over here.
Right.
So again we want to remove this.
So we have to connect it to this one.
So that is equal to node dot next.
All right.
So no dot next is four and no dot prev is two.
So what we are doing is two.
And then we are using the dot next operator.
Right.
So two next has to be the node with value four.
So that's what we are trying to do over here.
All right.
And after this we are going to check if the input node has a next value.
So if node dot next that is if node dot next is truthy.
Or if there is something after that.
So let's again take this example.
So over here we want to remove three.
So there is something in the next position right.
There is a node over here.
So what do we need to do.
We need to set the previous of this node to two over here.
Right.
So let's do that over here.
So node dot next.
Dot pref has to be set to node dot pref.
Again try to visualize this right.
So if you are clear visually what you're doing then the code comes naturally.
So over here node dot next is for dot prev.
So we are thinking about the pointer which is going from four to the left.
And that has to be set to no dot prev no dot prev.
Is this one over here.
All right.
So what we are trying to avoid over here is we don't want to have a situation where let's say node.prev
is null and then we say null dot next.
So we don't want that right.
So that's why we are checking whether node.prev is there.
If it is there then we are doing node dot prev dot.
Next is equal to node dot next.
Again remember the key is to visualize this.
So the code comes naturally.
And once we are done with this then just one more step remains.
So we have the node and now it's pointing somewhere right.
So we want to remove it.
So no dot next should be set to null.
And node.prev should be set to null and we are done with the code.
All right.
Now let's go ahead and run our code and see whether it's working or not.
So let's do that now.
Now over here we have linked list doubly and we have seen that it's uh, we have it in this structure
like we have null.
Then we have one, two, three, four, five and then we have null.
So this is the current way it looks.
Now let's say for example let's start by removing this two over here.
All right.
So let's do that.
So I'm calling linked list doubly and dot remove.
And I'm going to pass a node.
Right.
So that's what we are done over here.
We are passing a node as input to the remove function.
So let's say we wanted to remove the node with value two.
So that's this node over here.
So we are going to pass in the node two.
Okay, now let's run it and let's see what's happening.
Okay, so we have run it.
Now let's see how linked list doubly looks like now.
Now that we have run this and removed the node with value two.
So that should not be there.
So that's what we're going to check.
Now we have the head as one and the head is pointing to three, right?
Initially the head was one, was pointing to two, but now one is pointing to three.
So we have successfully removed two.
So it's working.
Now let's take an example where we want to remove the head.
Let's say we want to remove one.
So I'll change this to one and I'll run the code again.
Remember we are only removing one at this point right?
Again because we are running the code from the beginning again.
So we will again create this new doubly linked list, right?
The linked list.
We will be again created and we are going to remove only this node over here.
All right.
So the head should be two once we are done with this.
So let's go ahead and do that.
All right, so I've run the code.
Now let's check how it looks.
Okay so we have linked list W over here.
And when we open it we can see that the head is two and two is pointing to three.
And it goes on like that.
And the tail is five.
So yes we've successfully removed the head.
And lastly let's check a case where we remove the tail.
So the tail over here is five.
So let's again run our code.
And let's check how it looks.
All right.
So we have run the code.
Let's check it.
So over here you can see that the head is one and the tail is four right.
Initially the tail was five but we have successfully removed it.
And the tail over here is now four.
Now we have successfully coded the solution.
Now let's take a sample case and quickly walk through the code because you will be doing this in the
interview also.
So let's do that next.