Hi everyone!
So far we have written these two functions.
There is a function to remove a node from the doubly linked list, and another function to insert a
given node before another given node.
So these are the two functions that we have written so far.
Now let's proceed and write the next function, which is a function to remove all the nodes with a particular
value.
So all nodes with a particular value should be removed.
So that's what this function does.
All nodes value remove.
All right.
Now the value would be passed for this function.
Now because we already have our remove function.
We just need to identify the nodes where the value is equal to the value which is passed to us.
And then we can just call the remove function and remove those nodes.
All right.
So we will use a current uh variable.
Let's use let cur is equal to this dot head.
Now we will use this to traverse the doubly linked list.
So let's use a while loop while cur.
So as long as cur is not pointing to null, this will be truthy and it will keep looping.
Now when cur reaches null, which is at the end of the doubly linked list, we will break out of the
loop.
Now let's also have a temporary variable.
Let's call it temp and that should be equal to cur.
Now we need this because when we find that, let's say cur dot value is equal to the given value, we
cannot just remove cur right?
Because if you do that, we will not be able to go to the next node.
Right.
So we need to maintain the connection.
So that's why we are using a temporary variable.
And then let's set Cur is equal to cur.
Next.
And we will check if temp dot val is equal to the value which is passed to us, and if this is true,
then we will just call this dot remove.
And we will pass this particular node which is temp, right.
So there is no problem.
Even if you delete this, the connection is not lost because we have stored the next node already to
current.
All right.
Now that's the end of this function.
Now let's go ahead and call the linked list doubly.
Dot all nodes value remove.
And let's pass the value for and let's test our code.
All right.
So let's go ahead and run it.
And let's take a look at our doubly linked list.
And what's the expectation?
These three nodes should be removed and we should just have one pointing to five.
So let's go ahead and check it.
We have one which is pointing to five and that's pointing to null.
So yes we were able to successfully delete these three values.
And our algorithm is working.
Now let's take a quick sample input and see how the code is working.