Hi everyone.
Let's think in this video how we can solve this question.
Let's say that the given doubly linked list is this one over here.
And let's say the value given to us is equal to two.
So we need to remove all the nodes from this doubly linked list which have a value equal to two.
So what we will do is we will traverse the doubly linked list.
And for every node where the value is equal to two, we will call the remove function which we have
already written previously.
And we will just remove that node.
So let's look at how to traverse this doubly linked list in a smart manner, so that we don't lose the
connection when we remove a particular node.
Now we start with the head.
So this is the head.
Let's have a variable ker and let it point to the head.
All right.
Now let's create another variable temp and let it also point to ker.
All right.
Now let's move ker.
Let's move it ahead.
So ker moves ahead and temp is now over here.
Now we we check whether temp the value in temp is equal to the given value.
All right.
Now why do we do this.
Why do we have this mechanism.
This is done so that we don't lose the next value right.
For example if we did not have this temp value and let's say just ker was over here and we are checking
whether the current value is equal to the value given.
And if it's given, let's say we are passing that node.
In that case, let's say over here we have the value equal.
And let's say we did not have a temp variable.
Let's say this was not there.
So we are just having this ker pointer.
And we see that its value is the same as the given value.
And we pass this node and we remove this node.
If we do that then we will not be able to traverse the remaining part of the doubly linked list.
So that's why we need to avoid this.
And hence we are using another variable also.
All right.
So that we don't lose the next value.
Now once we have this arrangement we check the value in temp which is equal to one.
And we see that that value is not equal to the value which is given to us.
All right.
So we don't need to remove this node.
Now what we do is we move ker and temp ahead.
So how do we do that.
We we we already have ker as the next value.
Now in the loop.
In the next loop again we set temp to ker.
And then we set ker to car dot next.
So we move these two ahead.
So that's what happens basically.
And then we check the value of temp whether it's equal to the given value.
And we see it's equal.
So we pass temp.
We pass this node to the remove function.
So we do remove temp.
And that removes this node from the doubly linked list.
All right.
Now what we do we again move these two and we repeat this process.
We again check whether temp dot val is equal to the given value.
Yes that's true.
So we remove this node and this is removed.
Again we move this forward.
All right.
We again check whether the value is equal.
But over here it's not equal.
It's three over here.
And this is equal to two.
So we don't remove this node.
Again we move this forward and we check this value.
And this value they are equal.
So we remove this node as well.
And again we move this forward and we check and the value is equal.
So we remove this node as well.
All right.
And then finally we see that ker is equal to null.
So we will be looping as long as ker is truthy.
Now when ker is equal to null it will be false.
And we will come out of the loop.
Right.
And we will just be left with this part of the.
This is the new doubly linked list.
All the nodes with value two have been removed from it.
Now this is pretty straightforward.
The only thing that you need to keep in mind is when you remove, you should not lose the next value.
And that's why we were using a temp variable as well as the current variable.
All right.
Now let's quickly look at the time complexity and the space complexity of this solution.
Now over here you can see that we have to traverse the doubly linked list once right.
Therefore clearly the time complexity is O of n where n is the length of the doubly linked list because
we have to traverse it once, right?
We have to see every node once and check whether the value is equal to the given value.
And what about the space complexity?
We are not using any additional space, right?
So the space complexity is O of one or constant space.