1
00:00:02,210 --> 00:00:02,900
Hello, everyone.

2
00:00:02,930 --> 00:00:07,400
So today we are going to solve this problem, add two numbers as a list.

3
00:00:07,650 --> 00:00:13,450
OK, so basically we are giving dueling list, so linked list first and second.

4
00:00:13,850 --> 00:00:16,309
So this is our first link list two for three.

5
00:00:17,550 --> 00:00:21,570
So this is basically a number and the number is three, four, two.

6
00:00:21,660 --> 00:00:26,640
OK, so to obtain the number, what we have to do, we have to read the list in the reverse order.

7
00:00:26,790 --> 00:00:28,700
OK, so this is our first number.

8
00:00:29,250 --> 00:00:34,170
So this is number one and this is our second link list five, six, four.

9
00:00:34,320 --> 00:00:37,500
And to obtain the number, we have to read the link list in the reverse order.

10
00:00:37,800 --> 00:00:39,920
So basically this is four, six and five.

11
00:00:40,560 --> 00:00:42,310
So this is our number two.

12
00:00:42,960 --> 00:00:46,290
OK, now if we have to do we have to add these two numbers?

13
00:00:46,670 --> 00:00:51,810
OK, so after adding these two numbers, so that will be a simple, simple mathematical addition.

14
00:00:52,090 --> 00:00:55,470
So we will obtain five plus two, seven, six plus four is ten.

15
00:00:55,480 --> 00:00:58,940
So we will have one, three and four three one.

16
00:00:58,950 --> 00:00:59,990
So this will be eight.

17
00:01:00,870 --> 00:01:03,480
So this will be our answer is zero.

18
00:01:03,480 --> 00:01:05,010
Seven is the number.

19
00:01:05,190 --> 00:01:06,980
OK, and what will be the link list.

20
00:01:06,990 --> 00:01:11,050
So the link list will be eight zero seven.

21
00:01:11,240 --> 00:01:11,700
OK.

22
00:01:12,950 --> 00:01:14,630
So this should be our answer.

23
00:01:14,770 --> 00:01:15,140
OK.

24
00:01:16,120 --> 00:01:17,380
This should be evidence.

25
00:01:17,500 --> 00:01:23,050
This is the new linked list, and we have to add these two numbers and we have to return this new linked

26
00:01:23,050 --> 00:01:23,320
list.

27
00:01:24,240 --> 00:01:29,700
OK, so this is nothing, it is, but it is just basically an implementation problem.

28
00:01:29,780 --> 00:01:32,800
OK, so this is essentially an implementation problem.

29
00:01:33,570 --> 00:01:36,290
So what are the variables that we will need?

30
00:01:36,540 --> 00:01:40,460
So we will need a key variable in this case Wednesday, Kerry.

31
00:01:41,100 --> 00:01:41,480
OK.

32
00:01:41,880 --> 00:01:47,090
OK, so when we are adding these two numbers, six plus four, so we are getting 10 output.

33
00:01:47,310 --> 00:01:51,960
OK, we will store this zero and we will forward this one.

34
00:01:52,020 --> 00:01:55,120
OK, so basically for storing the zero.

35
00:01:55,140 --> 00:01:59,480
So let's say this number is an OK, OK, call it some.

36
00:02:00,030 --> 00:02:03,670
So for obtaining zero, what we will do some more often.

37
00:02:04,680 --> 00:02:06,060
So we will get zero.

38
00:02:06,390 --> 00:02:11,700
And for one basically forgety what we will do, we will do some by then.

39
00:02:12,140 --> 00:02:17,700
OK, so that's all we can obtain the carry and we can obtain their digits that we have to put.

40
00:02:18,210 --> 00:02:19,580
OK, so this is nothing.

41
00:02:19,590 --> 00:02:23,890
It is just an implementation problem and we have to handle some cases.

42
00:02:23,910 --> 00:02:26,040
OK, so what can be the cases.

43
00:02:26,040 --> 00:02:29,970
Cases can be the length of the list is different.

44
00:02:30,240 --> 00:02:35,700
For example, the length of the first list is greater than the length of the second linked list basically

45
00:02:35,700 --> 00:02:40,530
supports the first Linklaters two, four, three and seven.

46
00:02:41,250 --> 00:02:43,760
And the second link list is basically five, six, four.

47
00:02:44,770 --> 00:02:46,910
OK, these are the cases.

48
00:02:46,920 --> 00:02:50,400
Similarly, the length of the second linked list is greater.

49
00:02:50,520 --> 00:02:55,620
OK, so basically this is just an implementation problem and we have to handle some of the cases.

50
00:02:55,810 --> 00:03:02,220
OK, so what we have to do, we have to create this new long list and then we have to return this new

51
00:03:02,220 --> 00:03:02,680
link list.

52
00:03:02,970 --> 00:03:07,650
So this problem is basically a legal problem and we will write the code.

53
00:03:08,130 --> 00:03:11,730
OK, so first of all.

54
00:03:13,230 --> 00:03:20,340
If Azle, basically the first link doesn't exist, then what we can do better, the result so that Israel

55
00:03:20,360 --> 00:03:22,070
will be OK.

56
00:03:22,350 --> 00:03:23,070
Now if.

57
00:03:24,520 --> 00:03:25,930
The second link list is null.

58
00:03:28,730 --> 00:03:29,420
We can return.

59
00:03:29,840 --> 00:03:37,550
OK, now what I plan to do here is, OK, so what we will do, I will keep one pointer here and I will

60
00:03:37,550 --> 00:03:41,100
keep the second pointer here and then I will add these two numbers.

61
00:03:41,120 --> 00:03:42,740
OK, simple mathematical addition.

62
00:03:43,040 --> 00:03:44,390
So the output will be seven.

63
00:03:44,510 --> 00:03:46,370
OK, so the output will be seven.

64
00:03:46,580 --> 00:03:50,780
So I will make a NewLink list and I will start output is seven.

65
00:03:51,290 --> 00:03:52,370
OK then.

66
00:03:53,590 --> 00:03:59,050
This point will come here and this point will come here, then I will add these two numbers.

67
00:03:59,080 --> 00:04:04,550
So six plus four, which has been so I will told zero and I will get even.

68
00:04:04,600 --> 00:04:06,420
OK, so I will store zero.

69
00:04:06,430 --> 00:04:08,080
Basically I'm inserting Leithead.

70
00:04:08,320 --> 00:04:11,470
So this was my head of the NewLink list.

71
00:04:11,680 --> 00:04:13,540
So what I am doing, I will insert.

72
00:04:13,720 --> 00:04:15,040
I did OK.

73
00:04:15,050 --> 00:04:18,000
So this will be my scenario and the head will come here.

74
00:04:20,730 --> 00:04:27,960
Then what I will do so we have got equals one, OK, at this point, my guess is one, then it will

75
00:04:27,960 --> 00:04:29,280
come here and this will come in.

76
00:04:29,310 --> 00:04:31,110
So this is three and this is four.

77
00:04:31,350 --> 00:04:33,020
So I will add these two numbers.

78
00:04:33,030 --> 00:04:34,530
So four plus three seven.

79
00:04:34,530 --> 00:04:35,370
And we have one.

80
00:04:35,370 --> 00:04:37,070
Ghedi So the output will be eight.

81
00:04:37,380 --> 00:04:39,630
So I will put it in front of head.

82
00:04:39,850 --> 00:04:41,100
OK, and.

83
00:04:42,290 --> 00:04:43,630
My head will come here.

84
00:04:45,470 --> 00:04:52,010
OK, so this is my result, and number eight zero seven zero eight zero seven is the result number and

85
00:04:52,010 --> 00:04:54,560
but our output should be seven zero eight.

86
00:04:54,800 --> 00:05:00,300
So after getting this long list, what we have to do, we have to use the reverse function.

87
00:05:00,560 --> 00:05:04,970
OK, we will we will use the reverse function and then we will get our output.

88
00:05:05,380 --> 00:05:05,720
OK.

89
00:05:07,070 --> 00:05:10,580
OK, so this is how I am planning to solve this question basically.

90
00:05:12,470 --> 00:05:20,500
We will insert Leithead, and then after this, Linklaters ready, we will use the reverse function,

91
00:05:21,350 --> 00:05:24,170
OK, and everything else we can handle.

92
00:05:24,200 --> 00:05:25,700
OK, we can handle this case.

93
00:05:26,660 --> 00:05:27,760
We can handle this case.

94
00:05:27,770 --> 00:05:28,070
OK?

95
00:05:29,160 --> 00:05:31,990
Now, let us write the code, OK?

96
00:05:34,650 --> 00:05:37,100
So we need a carry variable.

97
00:05:38,120 --> 00:05:38,500
OK.

98
00:05:38,580 --> 00:05:40,080
Initially, the carry will be zero.

99
00:05:41,180 --> 00:05:43,120
And we also need a variable sum.

100
00:05:43,790 --> 00:05:45,530
OK, so.

101
00:05:46,880 --> 00:05:47,390
First.

102
00:05:48,970 --> 00:05:50,950
What we have to do, we have to run the loop.

103
00:05:52,670 --> 00:05:55,190
While both the linked list exist, OK?

104
00:05:56,670 --> 00:06:02,460
So while it is not questionable and while be is not, the question on this case is too.

105
00:06:03,300 --> 00:06:10,350
So, for example, my first linked list and my second linked list, let's say the first Linklaters went

106
00:06:10,350 --> 00:06:14,910
to and the second link listed three, four, five, six and seven.

107
00:06:15,120 --> 00:06:22,460
OK, so first of all, we have to solve this part separately and then we will solve this part separately.

108
00:06:22,500 --> 00:06:22,870
OK.

109
00:06:22,980 --> 00:06:24,840
So in this case, we will do so.

110
00:06:24,840 --> 00:06:30,360
We will add three plus one four, then four plus to six and then it will become null.

111
00:06:31,800 --> 00:06:33,710
OK, so when it becomes null.

112
00:06:33,750 --> 00:06:39,180
But we have to do we have to simply copy this part five, six and seven.

113
00:06:39,450 --> 00:06:41,730
OK, obviously we will take care of Keddie.

114
00:06:42,360 --> 00:06:46,800
So if there's any carry, for example, let's say this is two and this is nine, so nine plus two will

115
00:06:46,800 --> 00:06:47,370
be eleven.

116
00:06:48,090 --> 00:06:52,290
So I will put one and this one will be Carrizo five plus one.

117
00:06:52,950 --> 00:06:55,480
This will be six and then six, six and seven.

118
00:06:55,650 --> 00:06:59,360
So basically we will propagate the carry, OK, we will agree.

119
00:06:59,370 --> 00:07:03,670
But when the first list becomes null, then this is the scenario.

120
00:07:03,690 --> 00:07:05,230
This is the cases that we have to handle.

121
00:07:05,250 --> 00:07:07,680
OK, so this is basically an implementation problem.

122
00:07:08,140 --> 00:07:12,090
OK, just took care of cases and they will be able to solve it very easily.

123
00:07:12,350 --> 00:07:16,740
OK, and do not forget about the Keddie, OK, we have to propagate the carry.

124
00:07:16,740 --> 00:07:21,750
For example, if this was also nine, so nine plus one it will become then I was zero.

125
00:07:21,750 --> 00:07:22,680
I will propagate one.

126
00:07:22,950 --> 00:07:24,990
Let's say this is also nine, so nine plus one.

127
00:07:25,140 --> 00:07:28,290
Then I will propagate carry and seven plus one will be it.

128
00:07:28,750 --> 00:07:31,410
OK, so this is how I am planning to solve this problem.

129
00:07:31,590 --> 00:07:32,910
Do not forget about the Getty.

130
00:07:33,450 --> 00:07:39,960
So first I make I'm writing Leupp for this part while both the list exist.

131
00:07:40,180 --> 00:07:46,580
OK, while both the linked list exist, surveille is not external and welby's not externally.

132
00:07:46,920 --> 00:07:48,940
That means both the linked list exist.

133
00:07:49,890 --> 00:07:54,900
So what will be our sausen will be the value of it.

134
00:07:55,200 --> 00:07:57,540
OK, so this is you can see here value.

135
00:07:57,840 --> 00:08:02,650
So I am using value here, OK, and sometimes it can be data.

136
00:08:02,670 --> 00:08:03,870
OK, so.

137
00:08:05,150 --> 00:08:12,410
Value of A plus value of B, and please do not forget about the carry, so Careys initially zero, OK?

138
00:08:14,210 --> 00:08:20,510
But it will not always be zero, so after calculating the sum, what we have to do, we have to create

139
00:08:20,510 --> 00:08:21,100
a..

140
00:08:21,230 --> 00:08:22,910
OK, we have to create the node.

141
00:08:23,240 --> 00:08:23,720
So.

142
00:08:25,560 --> 00:08:27,060
List North Star and.

143
00:08:28,960 --> 00:08:31,150
We are creating an order to store the data.

144
00:08:31,180 --> 00:08:33,970
OK, so this will be new list.

145
00:08:34,900 --> 00:08:36,200
And what we have to do.

146
00:08:36,220 --> 00:08:38,440
We have discussed it will be some more 10.

147
00:08:39,539 --> 00:08:44,100
OK, and what will be our carry, so our carry will be basically some Martin.

148
00:08:46,120 --> 00:08:49,090
Now, we discussed that we will insert at the head.

149
00:08:50,210 --> 00:08:52,850
OK, so I had an integrated head pointer.

150
00:08:55,110 --> 00:09:01,110
So listen, start let's call it head to head, miss, this is the head of the original link, the link

151
00:09:01,380 --> 00:09:02,670
that I am creating.

152
00:09:03,090 --> 00:09:05,730
So initially, this is initially the new link.

153
00:09:05,740 --> 00:09:06,660
This doesn't exist.

154
00:09:07,910 --> 00:09:12,150
So we prepared, so I told you we will insert ahead.

155
00:09:12,470 --> 00:09:14,280
OK, so how to insert ahead?

156
00:09:14,330 --> 00:09:15,260
This is very simple.

157
00:09:17,590 --> 00:09:22,480
And arrow next to his head and then update the that so basically had the collision.

158
00:09:23,760 --> 00:09:25,800
OK, so we are inserting a TED.

159
00:09:28,740 --> 00:09:32,530
Since we are starting at the head position, we have to return.

160
00:09:32,730 --> 00:09:34,690
We have to reverse our linked list.

161
00:09:34,710 --> 00:09:37,080
OK, we have to reverse our final list.

162
00:09:38,940 --> 00:09:44,300
Now, at this point, what will happen, headline number 24, I lent number 25, what will happen?

163
00:09:44,640 --> 00:09:46,350
So when the list finished?

164
00:09:46,570 --> 00:09:49,150
OK, so one of the list doesn't exist anymore.

165
00:09:49,170 --> 00:09:49,830
OK, so.

166
00:09:50,840 --> 00:09:56,970
One thing I have to move forward in both the linked list so equals the next of.

167
00:09:58,350 --> 00:09:58,860
And.

168
00:10:00,510 --> 00:10:02,880
Because the next would be OK.

169
00:10:07,410 --> 00:10:14,220
Now, after this loop, one of the linked list doesn't exist, so as we discussed here, so if one of

170
00:10:14,220 --> 00:10:19,350
the link finished, we have to use we have to write the code for the remaining link list.

171
00:10:19,410 --> 00:10:21,390
OK, so while.

172
00:10:26,050 --> 00:10:29,210
While exist, so basically Wyly's not the questionable.

173
00:10:30,210 --> 00:10:34,190
So what we can do, we can cooperate this much, we can cooperate is good.

174
00:10:34,290 --> 00:10:34,620
OK.

175
00:10:39,990 --> 00:10:41,460
Control C and control V.

176
00:10:43,140 --> 00:10:43,560
Now.

177
00:10:44,570 --> 00:10:53,210
Since Bill doesn't exist, so I will remove B list North Star and I'm creating a new node, I am inserting

178
00:10:53,210 --> 00:10:53,670
it head.

179
00:10:53,940 --> 00:10:54,550
OK.

180
00:10:55,700 --> 00:11:00,660
Had a in charism by then, it was next off air and then we will move be.

181
00:11:03,100 --> 00:11:03,910
Now, if.

182
00:11:04,970 --> 00:11:10,880
So there are two scenarios either that you will finish or the B finishes, so if the B shorter than

183
00:11:10,880 --> 00:11:17,300
this call will be executed now if it finishes and B exist, so.

184
00:11:24,040 --> 00:11:25,760
Now, while is not the question.

185
00:11:25,780 --> 00:11:30,110
Basically, that means that B was longer and it was shorter.

186
00:11:30,130 --> 00:11:30,490
OK.

187
00:11:31,860 --> 00:11:33,180
So this will be.

188
00:11:34,900 --> 00:11:36,060
And this will be.

189
00:11:39,390 --> 00:11:40,500
These next to me.

190
00:11:42,150 --> 00:11:46,840
OK, now I think everything is fine, but one condition is still remaining.

191
00:11:47,070 --> 00:11:49,020
Now what if the carry is not zero?

192
00:11:49,440 --> 00:11:51,210
OK, so what if the cat is positive?

193
00:11:52,140 --> 00:11:59,100
So now at this point now suppose this was mine and this was one.

194
00:11:59,310 --> 00:12:00,510
So nine plus one is ten.

195
00:12:00,510 --> 00:12:04,560
So I will store zero and it will one will the carry.

196
00:12:04,710 --> 00:12:09,240
So if the cat is not zero, if the cat is positive, we have to copy the cat also.

197
00:12:09,720 --> 00:12:13,710
OK, so if the cat is not zero we have to copy the Kerry.

198
00:12:16,990 --> 00:12:18,940
So you can write if.

199
00:12:21,340 --> 00:12:22,840
Gary Esposito.

200
00:12:24,850 --> 00:12:27,010
Then again, you can copy this code.

201
00:12:36,460 --> 00:12:38,080
So basically, this will be.

202
00:12:40,120 --> 00:12:42,630
You can simply right, Gary, here, OK?

203
00:12:47,810 --> 00:12:50,540
OK, so everything is fine now what we have to do.

204
00:12:52,090 --> 00:12:54,040
We have to reverse our linked list.

205
00:12:54,370 --> 00:12:57,100
OK, now we have to reverse our linked list.

206
00:12:57,160 --> 00:13:01,840
So let us write the code for reversing that list because we were starting at the height position.

207
00:13:02,150 --> 00:13:05,560
OK, you can see here we are inserting added.

208
00:13:07,170 --> 00:13:07,500
OK.

209
00:13:09,900 --> 00:13:12,330
Now for reversing the gold, we need three pointer.

210
00:13:17,870 --> 00:13:20,240
First, the previous, which is initial initial.

211
00:13:23,200 --> 00:13:26,710
The second point, our current point, which is initially they had only.

212
00:13:29,840 --> 00:13:32,060
And we need a third appointment next.

213
00:13:32,180 --> 00:13:32,540
OK.

214
00:13:35,450 --> 00:13:37,630
So let us say trade or the linked list.

215
00:13:42,210 --> 00:13:44,220
So while current not to question questionable.

216
00:13:47,060 --> 00:13:55,070
Then the first step, save the next node, because I'm going to break that link, so I'm saving the

217
00:13:55,070 --> 00:13:55,670
next node.

218
00:14:00,060 --> 00:14:04,290
Now, let us break the link so the next of current.

219
00:14:05,370 --> 00:14:06,270
His previous.

220
00:14:10,650 --> 00:14:15,510
Now, let's move forward so far moving forward, previous equals current.

221
00:14:16,510 --> 00:14:17,740
And current Dick Wilson.

222
00:14:18,030 --> 00:14:26,130
OK, so we are moving forward and finally, the head of the linked list is nothing at previous, so

223
00:14:26,140 --> 00:14:27,550
let us update the head.

224
00:14:28,150 --> 00:14:31,330
So we had this previous and then you can return that.

225
00:14:32,840 --> 00:14:34,670
OK, so this is the computer called.

226
00:14:40,250 --> 00:14:45,860
So what is this time in the space complexity, so -- complexity is linear and the space complexity

227
00:14:46,400 --> 00:14:47,670
is questioned.

228
00:14:47,750 --> 00:14:49,610
OK, so constrained space.

229
00:14:55,440 --> 00:15:00,900
So what is happening here is, first of all, it is just an implementation problem, OK?

230
00:15:01,230 --> 00:15:05,340
It is just an implementation problem and we have to handle a few cases.

231
00:15:06,970 --> 00:15:14,080
OK, so this is very simple if one of the list doesn't exist, right, and the second link list, initially,

232
00:15:14,080 --> 00:15:14,830
the carrier zero.

233
00:15:15,100 --> 00:15:19,780
So this is head head is basically OK, this the head of the other.

234
00:15:20,020 --> 00:15:22,750
This is head of our linked list that we are making.

235
00:15:22,930 --> 00:15:29,230
OK, so while both the list exist, OK, so scenario can be like one linked list is bigger than the

236
00:15:29,230 --> 00:15:30,030
second link list.

237
00:15:30,400 --> 00:15:35,290
So if the list is, let's say one, two, three and the list BES one.

238
00:15:35,710 --> 00:15:38,200
So in this case I will add these two.

239
00:15:38,380 --> 00:15:43,480
OK, so some will be the addition of the value of the value of B, and obviously the carrier at this

240
00:15:43,480 --> 00:15:44,190
point is zero.

241
00:15:44,590 --> 00:15:46,150
I'm creating a new node.

242
00:15:46,360 --> 00:15:48,730
OK, so the new node will be one plus one, which is to.

243
00:15:50,310 --> 00:15:52,230
Next of kin is Head Harris, basically.

244
00:15:52,800 --> 00:15:54,840
So basically the next of twizzle.

245
00:15:56,120 --> 00:15:57,620
Then had a question.

246
00:15:57,650 --> 00:15:58,940
Basically, this is head.

247
00:16:00,880 --> 00:16:07,090
Then carries some Biton, so something is basically one plus one, too, so two by then, obviously

248
00:16:07,090 --> 00:16:10,240
this zero move and move be ahead.

249
00:16:10,450 --> 00:16:13,120
So he will come here and we will become null.

250
00:16:14,450 --> 00:16:21,530
We will become soullessness, B becomes Snel, this loop is over, so is not the question, we will

251
00:16:21,530 --> 00:16:25,490
execute this part and B Snell so we will not execute this part.

252
00:16:25,970 --> 00:16:31,760
So it is not externalism is the value of E-Plus Category zero and the value of phase two.

253
00:16:31,880 --> 00:16:33,770
And I'm inserting a different of head.

254
00:16:34,040 --> 00:16:36,830
So Head was pointing towards two.

255
00:16:38,340 --> 00:16:39,210
And this is Nell.

256
00:16:40,250 --> 00:16:41,870
So this is to.

257
00:16:44,300 --> 00:16:49,250
And next head so and then had a question, so basically this is head.

258
00:16:50,990 --> 00:16:56,610
And Gary's, again, zero, because to Bertani, zero, and then he will move ahead, so he will come

259
00:16:56,610 --> 00:17:01,370
back three, then three, two, and then the third will come here.

260
00:17:02,690 --> 00:17:08,510
And then it will become null after three, it will become null decisional and I will loop is over.

261
00:17:08,869 --> 00:17:13,819
So this is three to two, but we have to reverse this link list.

262
00:17:13,859 --> 00:17:17,240
So after reversing the long list, it will become, too.

263
00:17:18,220 --> 00:17:21,079
Two and three, and this is our output.

264
00:17:22,040 --> 00:17:26,650
OK, so that's how so this code is very simple.

265
00:17:26,680 --> 00:17:29,400
OK, so go to this code.

266
00:17:29,890 --> 00:17:31,870
So this reverse function is very simple.

267
00:17:32,590 --> 00:17:35,770
And then these are some cases that we have to handle.

268
00:17:36,350 --> 00:17:38,800
OK, some cases that we have to handle.

269
00:17:40,140 --> 00:17:41,920
And yeah, I think everything is fine.

270
00:17:41,940 --> 00:17:44,070
OK, so this is very this is very simple.

271
00:17:44,070 --> 00:17:44,350
Good.

272
00:17:44,560 --> 00:17:52,290
OK, so if you have any doubt in this problem, you can ask me, OK, I'm complexities linear and the

273
00:17:52,290 --> 00:17:54,540
space complexities, constraints based on space.

274
00:17:54,570 --> 00:17:54,900
OK.

275
00:17:55,990 --> 00:17:56,470
Thank you.

