1
00:00:00,790 --> 00:00:01,750
Hi everyone!

2
00:00:01,750 --> 00:00:08,650
So we have successfully coded the all nodes value remove function where a value is given, and in the

3
00:00:08,650 --> 00:00:12,250
doubly linked list, all the nodes with that particular value are removed.

4
00:00:12,280 --> 00:00:16,990
Now let's say this is the doubly linked list which is given to us, and let's say the value is given

5
00:00:16,990 --> 00:00:17,530
as two.

6
00:00:17,560 --> 00:00:18,640
So the value is two.

7
00:00:18,670 --> 00:00:20,590
So we need to remove these two right.

8
00:00:20,590 --> 00:00:22,000
So what's happening in this code.

9
00:00:22,000 --> 00:00:23,410
Let's quickly walk through it.

10
00:00:23,410 --> 00:00:26,620
Initially we have Cur is set to the head right.

11
00:00:26,620 --> 00:00:27,430
So this is the head.

12
00:00:27,430 --> 00:00:29,230
So cur is pointing to the head.

13
00:00:29,230 --> 00:00:34,090
Now we loop through the doubly linked list using the while loop while cur.

14
00:00:34,090 --> 00:00:38,140
So it will continue till cur comes to null because null is falsey.

15
00:00:38,140 --> 00:00:43,030
So when cur becomes equal to null and when we do this check, we will come out of this loop.

16
00:00:43,030 --> 00:00:45,580
Now over here we set temp is equal to cur.

17
00:00:45,580 --> 00:00:48,070
So temp is also set to cur over here.

18
00:00:48,070 --> 00:00:52,600
And then we move cur to the next value like cur is equal to cur dot next.

19
00:00:52,600 --> 00:00:54,010
So we move it over here.

20
00:00:54,010 --> 00:00:57,670
Now temp is pointing to one and cur is pointing to two.

21
00:00:57,670 --> 00:01:02,650
Right now we check whether the value of temp is equal to the given value, which is equal to two.

22
00:01:02,650 --> 00:01:04,480
Now we see it's not equal, right?

23
00:01:04,480 --> 00:01:05,560
So what do we do.

24
00:01:05,590 --> 00:01:07,840
We don't do this part right.

25
00:01:07,840 --> 00:01:11,530
And we come over here and we see cur is still truth.

26
00:01:11,530 --> 00:01:12,730
It's truthy right.

27
00:01:12,730 --> 00:01:13,840
So what do we do.

28
00:01:13,840 --> 00:01:15,610
We again set temp is equal to cur.

29
00:01:15,610 --> 00:01:16,120
So we move.

30
00:01:16,120 --> 00:01:17,470
This temp is mood.

31
00:01:17,470 --> 00:01:18,940
So temp is equal to cur.

32
00:01:18,940 --> 00:01:21,400
And again cur is set to cur dot next.

33
00:01:21,400 --> 00:01:22,660
So cur is moved.

34
00:01:22,930 --> 00:01:27,280
And now we again check whether the value in temp is equal to the given value.

35
00:01:27,280 --> 00:01:29,170
And in this case the value was two.

36
00:01:29,170 --> 00:01:30,700
And over here also we have two.

37
00:01:30,730 --> 00:01:34,330
So yes we call the remove function this dot remove temp.

38
00:01:34,330 --> 00:01:36,940
And this node over here is removed right.

39
00:01:36,940 --> 00:01:38,410
And there is no problem.

40
00:01:38,410 --> 00:01:41,290
We are not losing any data because this is stored right.

41
00:01:41,290 --> 00:01:44,710
We have the next we have whatever is next it's there.

42
00:01:44,710 --> 00:01:47,530
So when we remove this these two are connected together.

43
00:01:47,530 --> 00:01:47,920
Right.

44
00:01:47,920 --> 00:01:49,720
And cur now is over here.

45
00:01:50,260 --> 00:01:51,460
Now temp.

46
00:01:51,460 --> 00:01:55,120
Temp is now this node right at this point temp is this node.

47
00:01:55,120 --> 00:01:56,710
And that's pointing to null.

48
00:01:56,710 --> 00:01:58,720
And this is also pointing to null.

49
00:01:58,720 --> 00:02:00,550
So that's why we need temp right.

50
00:02:00,550 --> 00:02:05,620
If we did not have temp and if we just removed cur this would be cur and we would not be able to go

51
00:02:05,620 --> 00:02:06,340
further.

52
00:02:06,340 --> 00:02:06,790
Right.

53
00:02:06,790 --> 00:02:12,400
Because both of these are the next and previous of this node which is removed, which is removed using

54
00:02:12,400 --> 00:02:14,320
the function are now pointing to null.

55
00:02:14,350 --> 00:02:17,320
So that's why we have set car to car dot next.

56
00:02:17,320 --> 00:02:18,640
So that's not a problem.

57
00:02:18,640 --> 00:02:21,400
So we have removed this and again we come over here.

58
00:02:21,400 --> 00:02:22,810
Car is now still truthy.

59
00:02:22,810 --> 00:02:25,540
So we come inside and temp is set to cur.

60
00:02:25,540 --> 00:02:27,580
So temp is now set to cur again.

61
00:02:27,580 --> 00:02:29,950
And now cur is set to cur dot next.

62
00:02:29,950 --> 00:02:33,580
So we move cur again and we check whether the value is the same.

63
00:02:33,580 --> 00:02:35,800
So this is three and the given value was two.

64
00:02:35,800 --> 00:02:37,150
So these are not the same.

65
00:02:37,150 --> 00:02:39,370
So we don't go inside here.

66
00:02:39,370 --> 00:02:44,350
And again we come over here and cur is still truthy because it's pointing to a node.

67
00:02:44,350 --> 00:02:46,120
And we move temp.

68
00:02:46,120 --> 00:02:49,240
And again we move cur to car dot next over here.

69
00:02:49,240 --> 00:02:49,690
Right.

70
00:02:49,690 --> 00:02:51,130
So that's what we do over here.

71
00:02:51,130 --> 00:02:53,950
And then we check the value is the same we see.

72
00:02:53,950 --> 00:02:56,950
So we come inside and we remove this node.

73
00:02:56,950 --> 00:03:00,550
And then when we come over here we come out of the loop.

74
00:03:00,550 --> 00:03:02,800
Because at this point cur is pointing to null.

75
00:03:02,800 --> 00:03:04,690
And this is evaluating to false.

76
00:03:04,930 --> 00:03:07,390
And we come out and we have our solution.

77
00:03:07,390 --> 00:03:07,750
Right.

78
00:03:07,750 --> 00:03:09,700
One is now pointing to three.

79
00:03:09,700 --> 00:03:11,560
And it's also pointing back.

80
00:03:11,560 --> 00:03:16,270
And then these two are pointing to null which is the solution that we wanted.

81
00:03:16,270 --> 00:03:22,060
And we have seen that this runs in constant space because we are not using any additional space.

82
00:03:22,060 --> 00:03:28,000
But this requires O of n time because we need to traverse the given doubly linked list once.
