Hello, everyone.
So today we will solve the problem and the name of the problem is reverse Elementalist.
OK, so this is our original list.
So the original Linklaters one, two, three, four and five, and we have to reverse this original
linked list.
So after reversing the original link list, this will be our answer.
Five, four, three, two and one.
OK, so this is our input and this should be our output.
OK, we have to basically reverse a long list.
OK, so how we can reverse a list.
So this is very easy problem.
OK, so suppose this is the scenario.
OK, so these are the nodes of a long list.
OK, and suppose we have reversed some part of the linked list.
OK, so let's say the name of this node is previous node.
The name of this note is current law and let's say the name of this notice and OK, and this list goes
on.
OK, so here is Nell, OK, so let us assume we have already reversed this part of the linked list.
OK, now we have to reverse the remaining part of the linked list.
OK, so what we will do here is, first of all, we have three point previous, current and OK, so
we have three points previous, current.
And so the first step, first step, whatever what we will do, we will save this node.
OK, I have to save this node.
So.
North Star in.
What is in this is basically the next of the current A..
OK, so I created this point that and now what we have to do so after reversing what should happen,
this current pointer.
Should point towards this.
OK, so the second step is very simple, the next of current.
It should point towards the previous OK, and this node, this link will get broke, this link will
get broken.
OK, so this is our second step and our link will get broken at this point.
OK, now, if we have to do so, previous will become current.
OK, so this previous will come here.
OK, now previous is pointing towards current.
Then we will do current equals and OK so now current is pointing towards here.
OK, so this is current now and we have reversed.
This part of the linked list, OK, again, we will we will put discord inside a loop.
OK, so what will happen?
So this is current, this is previous and this will be over.
And again, we will break this link and we will make this link again.
This will become previous.
This will become current and it will go on, OK?
It will continue.
Now I am repeating one more time, so let's say.
This is my previous Naude.
This is my current north.
And this is my next nolde.
And let's see.
There's one more note and then the end of the linked list.
OK.
So the first step, we have to save this node, so for saving this node, I am creating appointer.
So Node started in which is basically the next of current.
OK, we have to save this node.
We have to save this node because we are going to break this link.
Now, the second step is break this link and make this link.
So basically.
Next of current.
It's pointing towards the previous.
OK, the next of current is pointing towards the previous node, then I will make previous here, OK,
previous will point towards the current node.
OK, so.
This is previous now and then what we have to do current will come here.
OK, so this will be current.
So current is basically an OK, so this end that we have saved.
So current is basically this OK.
Then again, we will we will execute the same code.
OK, so this is current, this is and this link will get broken.
I will make this link then it will become previous.
It will become current.
Then this will be end basically and will be null.
And then I will make I will break this link then it will pointier current next next of current is pointing
towards the previous then previous will come here and then current equals and was null.
So basically the current will become null.
OK.
So when the current becomes null.
So I will make the condition of the loop while current is not external.
So when current becomes null that means mailing list is reversed.
And this is our head.
OK, this is our head of the reverse blacklist headers.
Basically nothing at this previous only, OK?
It is basically nothing.
It is previous.
So after this loop.
Our head is nothing but previous.
OK, now what was the initial condition so initially?
So now let's say.
This is our original list, so basically current is nothing but head, this is previous, which is basically
nil.
So previous will be null initially, OK, and this will be our end.
OK, now this question is present.
Only told, OK, that it was linked list discussion is present to the record.
And now let us write the code.
OK, so we need three pointers.
OK, we need three pointer.
Previous, current and and so current pointer which is held initially.
We need.
Our previous point that.
Which is basically nil.
And we need appointer and OK, so first.
Let us ride the loop, so while current is not the questionable.
But they have to do we have to do four steps first, save the next node, OK?
First step, save the next order, because the next node, because we are going to break the link in
the next lane, so I have saved the next node and now we can break the link.
OK, so the next of clarinettist previous.
Now we have to move ahead.
OK, so for moving ahead, the current.
And current equals next, basically, and OK, next month, and and when the loop over, when the loop
is finished, basically the head of the basically the head of the reverse linked list is nothing but
previous.
So we will return previous and that's all OK.
Now, let's amid the cold, so what is the time and the space complexity of our solution so the time,
complexities, linear and the space complexities of an OK, so.
Ben, complexity is linear and space's constant all in space.
OK, so this was very easy problem.
OK, thank you.