1
00:00:00,860 --> 00:00:01,700
Hi everyone.

2
00:00:01,700 --> 00:00:04,040
So we have successfully coded the solution.

3
00:00:04,040 --> 00:00:09,320
Now let's walk through the code and again understand what's happening in each stages of the code.

4
00:00:09,320 --> 00:00:13,700
And again this is important because in any interview you will be asked to walk through the code.

5
00:00:13,700 --> 00:00:14,600
Once you have written.

6
00:00:14,600 --> 00:00:22,130
Now this part of the code and this part and this part of the code is just written so that we can test

7
00:00:22,130 --> 00:00:24,260
out the function once we have written it.

8
00:00:24,260 --> 00:00:24,650
All right.

9
00:00:24,650 --> 00:00:28,010
So this part of the code creates this doubly linked list.

10
00:00:28,010 --> 00:00:28,430
Right.

11
00:00:28,430 --> 00:00:32,330
Which is one pointing to two pointing to three to 4 to 5.

12
00:00:32,330 --> 00:00:33,680
And these two point to null.

13
00:00:33,680 --> 00:00:37,250
So the head is one and the tail is this one over here which is five.

14
00:00:37,250 --> 00:00:39,440
Now again quickly looking at the code.

15
00:00:39,440 --> 00:00:40,700
So we have a class node.

16
00:00:40,700 --> 00:00:45,590
With this class node we have created these five nodes 12345.

17
00:00:45,590 --> 00:00:46,010
Right.

18
00:00:46,010 --> 00:00:51,590
And then we are using the function link nodes where we pass two nodes to link them.

19
00:00:51,590 --> 00:00:51,830
Right.

20
00:00:51,830 --> 00:00:56,570
So node one dot next is set as node two and node two dot prev is set as node one.

21
00:00:56,570 --> 00:01:01,550
So using this we are uh and then here we are calling this function to connect these nodes.

22
00:01:01,550 --> 00:01:02,870
And then we have linked list.

23
00:01:02,870 --> 00:01:05,330
Doubly new is a new doubly linked list.

24
00:01:05,330 --> 00:01:07,550
And we have our doubly linked list class over here.

25
00:01:07,550 --> 00:01:10,100
So we are creating linked list doubly over here.

26
00:01:10,100 --> 00:01:12,890
And we are setting its head to one and tail to five.

27
00:01:12,890 --> 00:01:17,090
So this part of the code is used to create this doubly linked list.

28
00:01:17,090 --> 00:01:22,040
Now we have written the instance method remove node right.

29
00:01:22,040 --> 00:01:25,550
And the input to this function is a node.

30
00:01:25,550 --> 00:01:29,150
And we are calling it and we are passing five over here the node five.

31
00:01:29,150 --> 00:01:32,030
Now we are inside this method over here.

32
00:01:32,180 --> 00:01:32,900
All right.

33
00:01:32,900 --> 00:01:37,940
Now let's see the case where we are passing node five.

34
00:01:37,940 --> 00:01:40,460
Over here we can see that this is the tail right.

35
00:01:40,460 --> 00:01:42,920
So we find that this is the tail.

36
00:01:42,920 --> 00:01:45,980
And because we check whether this dot tail is the node.

37
00:01:45,980 --> 00:01:50,750
And it turns out true in this case because we are passing five over here, which is the tail.

38
00:01:50,750 --> 00:01:54,470
Now we are setting the tail the new tail as node dot pref.

39
00:01:54,470 --> 00:01:55,790
And this is the node right.

40
00:01:55,790 --> 00:01:56,840
This is the node.

41
00:01:56,840 --> 00:01:58,160
Let me just write that over here.

42
00:01:58,160 --> 00:01:58,970
This is the node.

43
00:01:58,970 --> 00:02:00,830
So no dot prev is this one.

44
00:02:00,830 --> 00:02:02,660
So this is node dot prev.

45
00:02:02,660 --> 00:02:05,480
And we are making this the new tail over here.

46
00:02:05,480 --> 00:02:06,020
Right.

47
00:02:06,020 --> 00:02:09,320
So over here then we are checking whether Node.prev is there.

48
00:02:09,320 --> 00:02:10,040
Yes it is there.

49
00:02:10,040 --> 00:02:11,240
Which is this node over here.

50
00:02:11,330 --> 00:02:12,200
Node four.

51
00:02:12,200 --> 00:02:12,680
All right.

52
00:02:12,680 --> 00:02:15,710
So we are setting node dot dot next.

53
00:02:15,710 --> 00:02:17,720
That is this connection over here.

54
00:02:18,290 --> 00:02:21,740
We are changing this connection over here to node dot next right.

55
00:02:21,740 --> 00:02:24,410
So node dot next over here is null.

56
00:02:26,450 --> 00:02:28,190
So this is node dot next.

57
00:02:28,190 --> 00:02:31,880
So we are changing this connection and making it point to over here.

58
00:02:32,030 --> 00:02:33,200
So that's done over here.

59
00:02:33,200 --> 00:02:33,650
Right.

60
00:02:33,650 --> 00:02:37,340
And then we are checking whether node dot next exists.

61
00:02:37,340 --> 00:02:40,070
This is node and node dot next is null.

62
00:02:40,070 --> 00:02:41,150
So this is falsy.

63
00:02:41,150 --> 00:02:42,740
So we don't go inside over here.

64
00:02:42,740 --> 00:02:47,480
And finally over here we said node dot next to null and node dot prev to null.

65
00:02:47,480 --> 00:02:47,780
Right.

66
00:02:47,780 --> 00:02:49,580
So we are removing node dot.

67
00:02:49,580 --> 00:02:52,400
Next is already null and node.prev.

68
00:02:52,400 --> 00:02:55,190
This connection is removed and it is set to null.

69
00:02:55,190 --> 00:02:57,380
And we are done with the solution right.

70
00:02:57,380 --> 00:03:01,490
So at the end of this we will have one and we have two.

71
00:03:01,520 --> 00:03:04,610
Then we have three and we have four.

72
00:03:04,940 --> 00:03:07,190
And this over here points to null.

73
00:03:08,210 --> 00:03:08,600
Right.

74
00:03:08,600 --> 00:03:16,310
This also points to null, and in the case of node five, it points to null in both the sites.

75
00:03:16,310 --> 00:03:21,950
So we have removed node five from the input doubly linked list and we have successfully done that.

76
00:03:21,950 --> 00:03:25,070
Now let's take a look where the input is the head.

77
00:03:25,070 --> 00:03:28,820
Now let's say over here we have dot remove and one we are passing the head.

78
00:03:28,820 --> 00:03:31,730
So over here we we would have identified it as the head.

79
00:03:31,730 --> 00:03:34,940
And we set the node dot next.

80
00:03:34,940 --> 00:03:36,350
So in this case this is node.

81
00:03:36,350 --> 00:03:39,500
So node dot next is set as the new head right.

82
00:03:39,500 --> 00:03:41,780
This dot head is node dot next.

83
00:03:41,780 --> 00:03:43,040
So this is the new head.

84
00:03:43,040 --> 00:03:46,550
Then over here we will check whether node.prev is there.

85
00:03:46,550 --> 00:03:48,260
So node dot prev is not there.

86
00:03:48,260 --> 00:03:52,760
So we don't come inside this if loop this this becomes falsy.

87
00:03:52,760 --> 00:03:55,940
Then we check if node dot next is there which is true.

88
00:03:55,940 --> 00:03:56,900
It's truthy right.

89
00:03:56,900 --> 00:03:57,440
It's there.

90
00:03:57,440 --> 00:04:00,590
So we say not dot next dot prev right.

91
00:04:00,590 --> 00:04:02,990
So this this connection over here.

92
00:04:02,990 --> 00:04:05,210
This connection over here is set node dot.

93
00:04:05,210 --> 00:04:05,510
Next.

94
00:04:05,510 --> 00:04:08,870
This is node dot next it's prev right.

95
00:04:08,870 --> 00:04:12,290
It's prev is set to node dot prev over here.

96
00:04:12,380 --> 00:04:14,630
And then finally over here.

97
00:04:14,630 --> 00:04:17,780
In this part we disconnect this connection right.

98
00:04:17,780 --> 00:04:22,250
So node dot next is set to null and node dot previous set to null.

99
00:04:22,250 --> 00:04:24,500
So at the end of this we have two.

100
00:04:24,500 --> 00:04:29,810
And that's pointing to three which is pointing to four which is pointing to five.

101
00:04:29,810 --> 00:04:31,220
This is pointing to null.

102
00:04:31,220 --> 00:04:32,810
This is also pointing to null.

103
00:04:32,810 --> 00:04:37,100
And in the case of node one both the sides point to null.

104
00:04:37,640 --> 00:04:40,970
So that's what's happening when we pass in the head.

105
00:04:40,970 --> 00:04:44,180
And let's say we pass the node three.

106
00:04:44,180 --> 00:04:45,800
In this case what happens.

107
00:04:46,400 --> 00:04:49,940
We identify that it's not the head and it's not the tail right.

108
00:04:49,940 --> 00:04:53,900
So we come over here we see that Node.prev is there which is this one.

109
00:04:53,900 --> 00:04:55,580
This is Node.prev.

110
00:04:56,250 --> 00:04:58,380
This is node and this is node next.

111
00:04:58,380 --> 00:04:58,680
Right.

112
00:04:58,680 --> 00:05:00,780
So node dot prev dot next.

113
00:05:00,780 --> 00:05:06,120
So this connection over here is set right to node dot next which is this one over here.

114
00:05:06,120 --> 00:05:09,870
And we see that node dot next which is this over here exists.

115
00:05:09,870 --> 00:05:13,590
So no dot next dot prev that is this connection over here.

116
00:05:13,590 --> 00:05:15,570
This connection over here is set right.

117
00:05:15,570 --> 00:05:18,690
We are changing this connection from here to here.

118
00:05:18,690 --> 00:05:21,030
And finally over here what do we do.

119
00:05:21,030 --> 00:05:23,850
We set node dot next and node.prev to null.

120
00:05:23,850 --> 00:05:25,860
So we remove these two connections.

121
00:05:25,860 --> 00:05:30,720
And we will get one that will point to two which will point to four.

122
00:05:30,720 --> 00:05:32,520
And this will point to five.

123
00:05:32,520 --> 00:05:35,130
And these two sides point to null.

124
00:05:35,460 --> 00:05:39,690
And then in the case of node three both the sides point to null.

125
00:05:39,690 --> 00:05:42,960
And yes, we have successfully coded this.

126
00:05:42,960 --> 00:05:47,490
And we have seen that this solution over here is O of one space and time.

127
00:05:47,490 --> 00:05:51,360
We are not using any extra space, and we are not traversing the doubly linked list.

128
00:05:51,360 --> 00:05:51,690
Right.

129
00:05:51,690 --> 00:05:54,390
The node over here we can see is given as input.

130
00:05:54,390 --> 00:05:55,980
So we don't need to traverse it.

131
00:05:55,980 --> 00:05:59,220
Therefore this operates in constant time as well.
