Hi everyone!
So we have successfully coded the all nodes value remove function where a value is given, and in the
doubly linked list, all the nodes with that particular value are removed.
Now let's say this is the doubly linked list which is given to us, and let's say the value is given
as two.
So the value is two.
So we need to remove these two right.
So what's happening in this code.
Let's quickly walk through it.
Initially we have Cur is set to the head right.
So this is the head.
So cur is pointing to the head.
Now we loop through the doubly linked list using the while loop while cur.
So it will continue till cur comes to null because null is falsey.
So when cur becomes equal to null and when we do this check, we will come out of this loop.
Now over here we set temp is equal to cur.
So temp is also set to cur over here.
And then we move cur to the next value like cur is equal to cur dot next.
So we move it over here.
Now temp is pointing to one and cur is pointing to two.
Right now we check whether the value of temp is equal to the given value, which is equal to two.
Now we see it's not equal, right?
So what do we do.
We don't do this part right.
And we come over here and we see cur is still truth.
It's truthy right.
So what do we do.
We again set temp is equal to cur.
So we move.
This temp is mood.
So temp is equal to cur.
And again cur is set to cur dot next.
So cur is moved.
And now we again check whether the value in temp is equal to the given value.
And in this case the value was two.
And over here also we have two.
So yes we call the remove function this dot remove temp.
And this node over here is removed right.
And there is no problem.
We are not losing any data because this is stored right.
We have the next we have whatever is next it's there.
So when we remove this these two are connected together.
Right.
And cur now is over here.
Now temp.
Temp is now this node right at this point temp is this node.
And that's pointing to null.
And this is also pointing to null.
So that's why we need temp right.
If we did not have temp and if we just removed cur this would be cur and we would not be able to go
further.
Right.
Because both of these are the next and previous of this node which is removed, which is removed using
the function are now pointing to null.
So that's why we have set car to car dot next.
So that's not a problem.
So we have removed this and again we come over here.
Car is now still truthy.
So we come inside and temp is set to cur.
So temp is now set to cur again.
And now cur is set to cur dot next.
So we move cur again and we check whether the value is the same.
So this is three and the given value was two.
So these are not the same.
So we don't go inside here.
And again we come over here and cur is still truthy because it's pointing to a node.
And we move temp.
And again we move cur to car dot next over here.
Right.
So that's what we do over here.
And then we check the value is the same we see.
So we come inside and we remove this node.
And then when we come over here we come out of the loop.
Because at this point cur is pointing to null.
And this is evaluating to false.
And we come out and we have our solution.
Right.
One is now pointing to three.
And it's also pointing back.
And then these two are pointing to null which is the solution that we wanted.
And we have seen that this runs in constant space because we are not using any additional space.
But this requires O of n time because we need to traverse the given doubly linked list once.