1
00:00:00,800 --> 00:00:03,320
Hi everyone, let's solve this question.

2
00:00:03,320 --> 00:00:06,470
You are given the head of a singly linked list.

3
00:00:06,470 --> 00:00:09,950
Write a function that will take the given head as input.

4
00:00:09,950 --> 00:00:14,930
Reverse the linked list and return the new head of the reversed linked list.

5
00:00:14,930 --> 00:00:16,460
All right, so this is the question.

6
00:00:16,460 --> 00:00:18,440
Now you are given a singly linked list.

7
00:00:18,440 --> 00:00:19,580
And the input.

8
00:00:19,580 --> 00:00:19,970
We are right.

9
00:00:19,970 --> 00:00:21,170
We have to write a function.

10
00:00:21,170 --> 00:00:24,260
The input to the function is the head of this singly linked list.

11
00:00:24,260 --> 00:00:26,630
And we have to reverse it and return the new head.

12
00:00:26,630 --> 00:00:27,710
So that's the question.

13
00:00:27,710 --> 00:00:33,110
Now remember in the interview when you are doing this, you can always ask clarifying questions so that

14
00:00:33,110 --> 00:00:35,150
you have a thorough understanding of the question.

15
00:00:35,150 --> 00:00:39,440
Now a possible question could be what if the input is only a single node?

16
00:00:39,440 --> 00:00:44,330
Now let's say the interviewer replies return the node itself at that point.

17
00:00:44,330 --> 00:00:44,900
All right.

18
00:00:44,930 --> 00:00:48,950
Now another possible question could be what should be returned if the input is null?

19
00:00:48,950 --> 00:00:50,930
Now let's say the interviewer replies.

20
00:00:50,930 --> 00:00:52,850
In this case, just return null.

21
00:00:53,180 --> 00:00:53,810
All right.

22
00:00:54,110 --> 00:01:00,080
Now the question is fairly straightforward, so probably no more clarifying questions are required.

23
00:01:00,080 --> 00:01:06,920
Now let's go ahead and write some test cases to identify if there are any particular edge cases that

24
00:01:06,920 --> 00:01:12,200
we need to take care, and to ensure that you and the interviewer are on the same page.

25
00:01:12,200 --> 00:01:18,200
Now remember, test cases are just various inputs and outputs that are expected in those cases.

26
00:01:18,200 --> 00:01:24,260
And when you write test cases, especially if the question is tricky, you can ask the interviewer whether

27
00:01:24,260 --> 00:01:27,200
it's okay that you come up together with some test cases.

28
00:01:27,200 --> 00:01:29,570
Now let's go ahead and write some test cases.

29
00:01:29,570 --> 00:01:33,530
Let's say the input is this singly linked list one.

30
00:01:33,530 --> 00:01:35,480
The node with value one goes to two.

31
00:01:35,510 --> 00:01:37,280
That goes to three that goes to four.

32
00:01:37,280 --> 00:01:39,140
And then that points to null.

33
00:01:39,140 --> 00:01:43,610
Now if this is the input the output should be 4321.

34
00:01:43,610 --> 00:01:44,840
And that points to null.

35
00:01:44,840 --> 00:01:51,410
Right now if the input is just a single node, then the output as we clarified, has to be just that

36
00:01:51,410 --> 00:01:52,820
particular single node.

37
00:01:52,820 --> 00:01:56,870
And if the input is null, then the output has to be just null.

38
00:01:57,350 --> 00:01:58,970
So that's pretty straightforward.

39
00:01:58,970 --> 00:02:03,770
Now let's go ahead and see how we can solve this question.

40
00:02:03,770 --> 00:02:07,550
And what logic or what method we can use to solve this question.
