Hello, everyone.
So today we are going to solve this problem, add two numbers as a list.
OK, so basically we are giving dueling list, so linked list first and second.
So this is our first link list two for three.
So this is basically a number and the number is three, four, two.
OK, so to obtain the number, what we have to do, we have to read the list in the reverse order.
OK, so this is our first number.
So this is number one and this is our second link list five, six, four.
And to obtain the number, we have to read the link list in the reverse order.
So basically this is four, six and five.
So this is our number two.
OK, now if we have to do we have to add these two numbers?
OK, so after adding these two numbers, so that will be a simple, simple mathematical addition.
So we will obtain five plus two, seven, six plus four is ten.
So we will have one, three and four three one.
So this will be eight.
So this will be our answer is zero.
Seven is the number.
OK, and what will be the link list.
So the link list will be eight zero seven.
OK.
So this should be our answer.
OK.
This should be evidence.
This is the new linked list, and we have to add these two numbers and we have to return this new linked
list.
OK, so this is nothing, it is, but it is just basically an implementation problem.
OK, so this is essentially an implementation problem.
So what are the variables that we will need?
So we will need a key variable in this case Wednesday, Kerry.
OK.
OK, so when we are adding these two numbers, six plus four, so we are getting 10 output.
OK, we will store this zero and we will forward this one.
OK, so basically for storing the zero.
So let's say this number is an OK, OK, call it some.
So for obtaining zero, what we will do some more often.
So we will get zero.
And for one basically forgety what we will do, we will do some by then.
OK, so that's all we can obtain the carry and we can obtain their digits that we have to put.
OK, so this is nothing.
It is just an implementation problem and we have to handle some cases.
OK, so what can be the cases.
Cases can be the length of the list is different.
For example, the length of the first list is greater than the length of the second linked list basically
supports the first Linklaters two, four, three and seven.
And the second link list is basically five, six, four.
OK, these are the cases.
Similarly, the length of the second linked list is greater.
OK, so basically this is just an implementation problem and we have to handle some of the cases.
OK, so what we have to do, we have to create this new long list and then we have to return this new
link list.
So this problem is basically a legal problem and we will write the code.
OK, so first of all.
If Azle, basically the first link doesn't exist, then what we can do better, the result so that Israel
will be OK.
Now if.
The second link list is null.
We can return.
OK, now what I plan to do here is, OK, so what we will do, I will keep one pointer here and I will
keep the second pointer here and then I will add these two numbers.
OK, simple mathematical addition.
So the output will be seven.
OK, so the output will be seven.
So I will make a NewLink list and I will start output is seven.
OK then.
This point will come here and this point will come here, then I will add these two numbers.
So six plus four, which has been so I will told zero and I will get even.
OK, so I will store zero.
Basically I'm inserting Leithead.
So this was my head of the NewLink list.
So what I am doing, I will insert.
I did OK.
So this will be my scenario and the head will come here.
Then what I will do so we have got equals one, OK, at this point, my guess is one, then it will
come here and this will come in.
So this is three and this is four.
So I will add these two numbers.
So four plus three seven.
And we have one.
Ghedi So the output will be eight.
So I will put it in front of head.
OK, and.
My head will come here.
OK, so this is my result, and number eight zero seven zero eight zero seven is the result number and
but our output should be seven zero eight.
So after getting this long list, what we have to do, we have to use the reverse function.
OK, we will we will use the reverse function and then we will get our output.
OK.
OK, so this is how I am planning to solve this question basically.
We will insert Leithead, and then after this, Linklaters ready, we will use the reverse function,
OK, and everything else we can handle.
OK, we can handle this case.
We can handle this case.
OK?
Now, let us write the code, OK?
So we need a carry variable.
OK.
Initially, the carry will be zero.
And we also need a variable sum.
OK, so.
First.
What we have to do, we have to run the loop.
While both the linked list exist, OK?
So while it is not questionable and while be is not, the question on this case is too.
So, for example, my first linked list and my second linked list, let's say the first Linklaters went
to and the second link listed three, four, five, six and seven.
OK, so first of all, we have to solve this part separately and then we will solve this part separately.
OK.
So in this case, we will do so.
We will add three plus one four, then four plus to six and then it will become null.
OK, so when it becomes null.
But we have to do we have to simply copy this part five, six and seven.
OK, obviously we will take care of Keddie.
So if there's any carry, for example, let's say this is two and this is nine, so nine plus two will
be eleven.
So I will put one and this one will be Carrizo five plus one.
This will be six and then six, six and seven.
So basically we will propagate the carry, OK, we will agree.
But when the first list becomes null, then this is the scenario.
This is the cases that we have to handle.
OK, so this is basically an implementation problem.
OK, just took care of cases and they will be able to solve it very easily.
OK, and do not forget about the Keddie, OK, we have to propagate the carry.
For example, if this was also nine, so nine plus one it will become then I was zero.
I will propagate one.
Let's say this is also nine, so nine plus one.
Then I will propagate carry and seven plus one will be it.
OK, so this is how I am planning to solve this problem.
Do not forget about the Getty.
So first I make I'm writing Leupp for this part while both the list exist.
OK, while both the linked list exist, surveille is not external and welby's not externally.
That means both the linked list exist.
So what will be our sausen will be the value of it.
OK, so this is you can see here value.
So I am using value here, OK, and sometimes it can be data.
OK, so.
Value of A plus value of B, and please do not forget about the carry, so Careys initially zero, OK?
But it will not always be zero, so after calculating the sum, what we have to do, we have to create
a..
OK, we have to create the node.
So.
List North Star and.
We are creating an order to store the data.
OK, so this will be new list.
And what we have to do.
We have discussed it will be some more 10.
OK, and what will be our carry, so our carry will be basically some Martin.
Now, we discussed that we will insert at the head.
OK, so I had an integrated head pointer.
So listen, start let's call it head to head, miss, this is the head of the original link, the link
that I am creating.
So initially, this is initially the new link.
This doesn't exist.
So we prepared, so I told you we will insert ahead.
OK, so how to insert ahead?
This is very simple.
And arrow next to his head and then update the that so basically had the collision.
OK, so we are inserting a TED.
Since we are starting at the head position, we have to return.
We have to reverse our linked list.
OK, we have to reverse our final list.
Now, at this point, what will happen, headline number 24, I lent number 25, what will happen?
So when the list finished?
OK, so one of the list doesn't exist anymore.
OK, so.
One thing I have to move forward in both the linked list so equals the next of.
And.
Because the next would be OK.
Now, after this loop, one of the linked list doesn't exist, so as we discussed here, so if one of
the link finished, we have to use we have to write the code for the remaining link list.
OK, so while.
While exist, so basically Wyly's not the questionable.
So what we can do, we can cooperate this much, we can cooperate is good.
OK.
Control C and control V.
Now.
Since Bill doesn't exist, so I will remove B list North Star and I'm creating a new node, I am inserting
it head.
OK.
Had a in charism by then, it was next off air and then we will move be.
Now, if.
So there are two scenarios either that you will finish or the B finishes, so if the B shorter than
this call will be executed now if it finishes and B exist, so.
Now, while is not the question.
Basically, that means that B was longer and it was shorter.
OK.
So this will be.
And this will be.
These next to me.
OK, now I think everything is fine, but one condition is still remaining.
Now what if the carry is not zero?
OK, so what if the cat is positive?
So now at this point now suppose this was mine and this was one.
So nine plus one is ten.
So I will store zero and it will one will the carry.
So if the cat is not zero, if the cat is positive, we have to copy the cat also.
OK, so if the cat is not zero we have to copy the Kerry.
So you can write if.
Gary Esposito.
Then again, you can copy this code.
So basically, this will be.
You can simply right, Gary, here, OK?
OK, so everything is fine now what we have to do.
We have to reverse our linked list.
OK, now we have to reverse our linked list.
So let us write the code for reversing that list because we were starting at the height position.
OK, you can see here we are inserting added.
OK.
Now for reversing the gold, we need three pointer.
First, the previous, which is initial initial.
The second point, our current point, which is initially they had only.
And we need a third appointment next.
OK.
So let us say trade or the linked list.
So while current not to question questionable.
Then the first step, save the next node, because I'm going to break that link, so I'm saving the
next node.
Now, let us break the link so the next of current.
His previous.
Now, let's move forward so far moving forward, previous equals current.
And current Dick Wilson.
OK, so we are moving forward and finally, the head of the linked list is nothing at previous, so
let us update the head.
So we had this previous and then you can return that.
OK, so this is the computer called.
So what is this time in the space complexity, so -- complexity is linear and the space complexity
is questioned.
OK, so constrained space.
So what is happening here is, first of all, it is just an implementation problem, OK?
It is just an implementation problem and we have to handle a few cases.
OK, so this is very simple if one of the list doesn't exist, right, and the second link list, initially,
the carrier zero.
So this is head head is basically OK, this the head of the other.
This is head of our linked list that we are making.
OK, so while both the list exist, OK, so scenario can be like one linked list is bigger than the
second link list.
So if the list is, let's say one, two, three and the list BES one.
So in this case I will add these two.
OK, so some will be the addition of the value of the value of B, and obviously the carrier at this
point is zero.
I'm creating a new node.
OK, so the new node will be one plus one, which is to.
Next of kin is Head Harris, basically.
So basically the next of twizzle.
Then had a question.
Basically, this is head.
Then carries some Biton, so something is basically one plus one, too, so two by then, obviously
this zero move and move be ahead.
So he will come here and we will become null.
We will become soullessness, B becomes Snel, this loop is over, so is not the question, we will
execute this part and B Snell so we will not execute this part.
So it is not externalism is the value of E-Plus Category zero and the value of phase two.
And I'm inserting a different of head.
So Head was pointing towards two.
And this is Nell.
So this is to.
And next head so and then had a question, so basically this is head.
And Gary's, again, zero, because to Bertani, zero, and then he will move ahead, so he will come
back three, then three, two, and then the third will come here.
And then it will become null after three, it will become null decisional and I will loop is over.
So this is three to two, but we have to reverse this link list.
So after reversing the long list, it will become, too.
Two and three, and this is our output.
OK, so that's how so this code is very simple.
OK, so go to this code.
So this reverse function is very simple.
And then these are some cases that we have to handle.
OK, some cases that we have to handle.
And yeah, I think everything is fine.
OK, so this is very this is very simple.
Good.
OK, so if you have any doubt in this problem, you can ask me, OK, I'm complexities linear and the
space complexities, constraints based on space.
OK.
Thank you.