1
00:00:00,860 --> 00:00:01,760
Hi everyone!

2
00:00:01,760 --> 00:00:04,070
So far we have written these two functions.

3
00:00:04,070 --> 00:00:09,530
There is a function to remove a node from the doubly linked list, and another function to insert a

4
00:00:09,530 --> 00:00:12,110
given node before another given node.

5
00:00:12,110 --> 00:00:14,930
So these are the two functions that we have written so far.

6
00:00:14,930 --> 00:00:21,440
Now let's proceed and write the next function, which is a function to remove all the nodes with a particular

7
00:00:21,440 --> 00:00:22,010
value.

8
00:00:22,010 --> 00:00:26,360
So all nodes with a particular value should be removed.

9
00:00:26,360 --> 00:00:28,760
So that's what this function does.

10
00:00:28,850 --> 00:00:30,530
All nodes value remove.

11
00:00:30,530 --> 00:00:31,070
All right.

12
00:00:31,070 --> 00:00:35,120
Now the value would be passed for this function.

13
00:00:35,240 --> 00:00:38,930
Now because we already have our remove function.

14
00:00:38,930 --> 00:00:44,510
We just need to identify the nodes where the value is equal to the value which is passed to us.

15
00:00:44,510 --> 00:00:48,560
And then we can just call the remove function and remove those nodes.

16
00:00:48,560 --> 00:00:49,160
All right.

17
00:00:49,160 --> 00:00:53,270
So we will use a current uh variable.

18
00:00:53,270 --> 00:00:56,420
Let's use let cur is equal to this dot head.

19
00:00:56,570 --> 00:00:59,750
Now we will use this to traverse the doubly linked list.

20
00:00:59,750 --> 00:01:02,360
So let's use a while loop while cur.

21
00:01:02,480 --> 00:01:07,670
So as long as cur is not pointing to null, this will be truthy and it will keep looping.

22
00:01:07,670 --> 00:01:13,010
Now when cur reaches null, which is at the end of the doubly linked list, we will break out of the

23
00:01:13,010 --> 00:01:13,430
loop.

24
00:01:13,430 --> 00:01:16,190
Now let's also have a temporary variable.

25
00:01:16,190 --> 00:01:19,130
Let's call it temp and that should be equal to cur.

26
00:01:19,250 --> 00:01:25,340
Now we need this because when we find that, let's say cur dot value is equal to the given value, we

27
00:01:25,340 --> 00:01:26,780
cannot just remove cur right?

28
00:01:26,780 --> 00:01:30,350
Because if you do that, we will not be able to go to the next node.

29
00:01:30,350 --> 00:01:30,770
Right.

30
00:01:30,770 --> 00:01:32,600
So we need to maintain the connection.

31
00:01:32,600 --> 00:01:35,300
So that's why we are using a temporary variable.

32
00:01:35,300 --> 00:01:37,220
And then let's set Cur is equal to cur.

33
00:01:37,430 --> 00:01:38,090
Next.

34
00:01:38,980 --> 00:01:48,550
And we will check if temp dot val is equal to the value which is passed to us, and if this is true,

35
00:01:48,550 --> 00:01:51,520
then we will just call this dot remove.

36
00:01:52,580 --> 00:01:57,200
And we will pass this particular node which is temp, right.

37
00:01:57,200 --> 00:01:58,430
So there is no problem.

38
00:01:58,430 --> 00:02:03,680
Even if you delete this, the connection is not lost because we have stored the next node already to

39
00:02:03,680 --> 00:02:04,130
current.

40
00:02:04,130 --> 00:02:04,670
All right.

41
00:02:04,670 --> 00:02:07,460
Now that's the end of this function.

42
00:02:08,400 --> 00:02:12,090
Now let's go ahead and call the linked list doubly.

43
00:02:13,960 --> 00:02:16,990
Dot all nodes value remove.

44
00:02:16,990 --> 00:02:21,220
And let's pass the value for and let's test our code.

45
00:02:21,250 --> 00:02:21,640
All right.

46
00:02:21,640 --> 00:02:23,260
So let's go ahead and run it.

47
00:02:25,680 --> 00:02:28,710
And let's take a look at our doubly linked list.

48
00:02:29,310 --> 00:02:30,930
And what's the expectation?

49
00:02:30,930 --> 00:02:35,490
These three nodes should be removed and we should just have one pointing to five.

50
00:02:35,490 --> 00:02:36,990
So let's go ahead and check it.

51
00:02:36,990 --> 00:02:42,090
We have one which is pointing to five and that's pointing to null.

52
00:02:42,090 --> 00:02:46,380
So yes we were able to successfully delete these three values.

53
00:02:46,380 --> 00:02:48,600
And our algorithm is working.

54
00:02:48,600 --> 00:02:54,780
Now let's take a quick sample input and see how the code is working.
