Hi everyone, let's solve this question.
You are given the head of a singly linked list.
Write a function that will take the given head as input.
Reverse the linked list and return the new head of the reversed linked list.
All right, so this is the question.
Now you are given a singly linked list.
And the input.
We are right.
We have to write a function.
The input to the function is the head of this singly linked list.
And we have to reverse it and return the new head.
So that's the question.
Now remember in the interview when you are doing this, you can always ask clarifying questions so that
you have a thorough understanding of the question.
Now a possible question could be what if the input is only a single node?
Now let's say the interviewer replies return the node itself at that point.
All right.
Now another possible question could be what should be returned if the input is null?
Now let's say the interviewer replies.
In this case, just return null.
All right.
Now the question is fairly straightforward, so probably no more clarifying questions are required.
Now let's go ahead and write some test cases to identify if there are any particular edge cases that
we need to take care, and to ensure that you and the interviewer are on the same page.
Now remember, test cases are just various inputs and outputs that are expected in those cases.
And when you write test cases, especially if the question is tricky, you can ask the interviewer whether
it's okay that you come up together with some test cases.
Now let's go ahead and write some test cases.
Let's say the input is this singly linked list one.
The node with value one goes to two.
That goes to three that goes to four.
And then that points to null.
Now if this is the input the output should be 4321.
And that points to null.
Right now if the input is just a single node, then the output as we clarified, has to be just that
particular single node.
And if the input is null, then the output has to be just null.
So that's pretty straightforward.
Now let's go ahead and see how we can solve this question.
And what logic or what method we can use to solve this question.