Hi everyone.
So we have successfully coded the solution.
Now let's quickly take a sample input and walk through the code that we've written to understand what's
happening.
Now.
This part of the code is just written so that we can test the function that we are writing over here.
Right.
So this is a node class right.
So using this we are creating a node with value one.
And we are saying that the next next uh the pointer of this node is pointing to the node with value
two.
So one is pointing to two and two is pointing to three and three is pointing to four.
So this part creates this singly linked list.
And we are going to pass the head which is one, into the reverse linked list function which we have
written.
So we are going to pass this particular node over here.
Now at this point previous is set to null.
So I'm just calling previous as P.
And there's nothing over here.
So let's take this as null and current is set to the head.
The head is one over here.
So that's what we have done over here.
Current is head.
Now we are going to have this while loop right.
And we are going to repeat this.
As long as current is not equal to null or we will stop this when current reaches over here.
Right when current reaches over here, it will be equal to null and we will stop the while loop.
All right.
Now previous is null.
Current is the node one.
Now we are inside the while loop and we define another uh pointer next which is pointing to current
dot next right.
So current dot next is two.
So next is at two.
Right now we have stored this.
So it's safe to severe this connection.
So we will break this connection.
And we will make one point to previous or null right.
We'll make this node point to previous which is what we're doing over here.
Current dot next is equal to previous.
And at this point previous is null right.
So we are getting one pointing to null.
So that's what we are getting over here.
And then over here we are setting previous to current.
So previous is moved over here.
And current is set to next.
So current will be moved over here.
So we are moving previous and current and we are done with one iteration of the while loop.
Now we are going to check again whether current is null.
So current is not null.
So we again do this right.
So we will find next.
And we keep doing this again and again.
And we will do this till current reaches over here.
Right.
We'll do this loop till current reaches over here.
So in the next loop we will have previous over here current over here.
And we will have next over here.
Then we will remove this connection and we will make current point to previous.
Right.
So we will make two point to one.
So we'll get two pointing to one pointing to null.
And then we'll add three over here.
Right.
We will again move previous current next.
And we will make current point to previous.
So we will have three pointing to two pointing to one pointing to null.
And after that again we'll have four pointing to two pointing to three pointing to one.
And at this point when we do current is next.
Next would be null.
Right?
When current is over here, next is null.
And then we over here we are doing current equal to next.
So current will become null.
And at this point the while loop stops and we just return previous right.
Previous is this node over here which is the head of the reverse singly linked list.
And that's our answer.
So that's what we had to do.
And we are able to successfully do it over here.
And the time complexity of this solution is O of n, because we have to traverse the singly linked list
one time.
And the space complexity of this solution is O of one or constant space.
Because we are not creating a new linked list.
Right?
We are.
We are just having a few pointers and we are changing the direction, uh, in which existing nodes in
the memory are pointing.
And those are the input.
That's the input.
Right.
So we are not using any extra space.
So the space complexity of this solution is O of one.