1
00:00:00,720 --> 00:00:03,150
Hi everyone, let's do this question.

2
00:00:03,150 --> 00:00:09,420
You are given two non-empty linked lists representing two non-negative integers.

3
00:00:09,420 --> 00:00:15,780
The digits are stored in reverse order, and each of their nodes contains a single digit.

4
00:00:15,780 --> 00:00:19,440
Add the two numbers and return the sum as a linked list.

5
00:00:19,440 --> 00:00:21,690
So that's the question and why it's mentioned.

6
00:00:21,690 --> 00:00:29,040
You may assume the two numbers do not contain any leading zero except the number zero itself, and then

7
00:00:29,040 --> 00:00:31,440
the value of a node is between 0 and 9.

8
00:00:31,440 --> 00:00:31,890
All right.

9
00:00:31,890 --> 00:00:33,720
So again that's mentioned over here as well.

10
00:00:33,720 --> 00:00:36,150
So it's a single digit the value if every node.

11
00:00:36,180 --> 00:00:42,720
Now the question says that we are given two non-empty linked lists and they are non-negative integers.

12
00:00:42,720 --> 00:00:46,440
And then the value is stored in reverse order.

13
00:00:46,440 --> 00:00:48,270
So again let's try to understand that.

14
00:00:48,270 --> 00:00:52,710
So for example if the numbers that we want to add because we have to add them right.

15
00:00:52,710 --> 00:01:01,800
So if the numbers that we want to add add are 123 and let's say 751 now because the digits are stored

16
00:01:01,800 --> 00:01:04,920
in reverse order, the linked list will look like this.

17
00:01:04,920 --> 00:01:07,440
We'll have three going to two going to one.

18
00:01:07,440 --> 00:01:12,720
So that's the reverse order right from three to 2 to 1 and then one to 5 to 7.

19
00:01:12,720 --> 00:01:14,460
So that's the reverse order.

20
00:01:14,460 --> 00:01:16,050
That is what is mentioned over here.

21
00:01:16,050 --> 00:01:21,090
Now we need to add these up together and then return the sum as a linked list.

22
00:01:21,090 --> 00:01:25,860
So for example over here if we add these two we will get 874 right.

23
00:01:25,860 --> 00:01:29,880
So we have to return this which is the sum as a linked list.

24
00:01:29,880 --> 00:01:32,340
So again that should be in the reverse order.

25
00:01:32,340 --> 00:01:34,290
So we will have four going to seven.

26
00:01:34,290 --> 00:01:35,820
And then that's going to eight.

27
00:01:35,820 --> 00:01:37,440
So that is this order over here.

28
00:01:37,950 --> 00:01:39,210
So that's the question over here.

29
00:01:39,210 --> 00:01:44,220
And then it's mentioned that you may assume the two numbers do not contain any leading zero.

30
00:01:44,220 --> 00:01:47,340
So there won't be a case like 074.

31
00:01:47,340 --> 00:01:53,340
Like for 74 it would be just 74 not 074, except the number zero itself.

32
00:01:53,340 --> 00:01:57,630
So that this is a possibility 000 because that's just the number zero.

33
00:01:57,630 --> 00:01:58,950
Now this is the question.

34
00:01:58,950 --> 00:02:00,600
Now it's pretty straightforward.

35
00:02:00,600 --> 00:02:06,330
Now let's go ahead and look at a few test cases for this so that we get a good understanding of this

36
00:02:06,330 --> 00:02:06,960
question.

37
00:02:07,410 --> 00:02:12,000
Now let's say we want to add 540 and 723.

38
00:02:12,000 --> 00:02:16,050
Now when we add them we will get 1263.

39
00:02:16,050 --> 00:02:20,040
So this has to be returned as a linked list in the reverse order.

40
00:02:20,040 --> 00:02:23,130
So we have three going to six going to two going to one.

41
00:02:23,130 --> 00:02:28,110
Now the interesting thing to notice over here is there is nothing over here in these two linked lists.

42
00:02:28,110 --> 00:02:30,810
But in the output we have four nodes.

43
00:02:30,810 --> 00:02:32,640
That's something interesting to note.

44
00:02:32,640 --> 00:02:35,640
Now what if we want to add 99 and 16.

45
00:02:35,640 --> 00:02:37,530
Again it's going to be in the reverse order.

46
00:02:37,530 --> 00:02:40,530
So we have nine going to nine and then six going to one.

47
00:02:40,530 --> 00:02:43,170
And when we add them we will get 115.

48
00:02:43,170 --> 00:02:45,540
So the output also is in the reverse order.

49
00:02:45,540 --> 00:02:47,820
So five goes to one and that goes to one.

50
00:02:47,820 --> 00:02:49,950
So this is the output linked list.

51
00:02:49,980 --> 00:02:53,250
Now what if we want to add 729 and 53.

52
00:02:53,250 --> 00:02:57,750
Now you can see the length of the two linked lists need not be the same.

53
00:02:57,750 --> 00:02:58,950
Again we can add them.

54
00:02:58,950 --> 00:03:00,570
We will get 782.

55
00:03:00,570 --> 00:03:02,070
And that would be the output.

56
00:03:02,070 --> 00:03:04,350
So two goes to eight and that goes to seven.

57
00:03:04,350 --> 00:03:05,700
So that's the reverse order.

58
00:03:05,700 --> 00:03:09,120
And then let's say one linked list is null.

59
00:03:09,120 --> 00:03:10,650
So there is nothing over here.

60
00:03:10,650 --> 00:03:12,810
And then we have zero going to one.

61
00:03:12,810 --> 00:03:14,040
So that's actually ten right.

62
00:03:14,040 --> 00:03:15,270
So ten plus nothing.

63
00:03:15,270 --> 00:03:16,950
So that gives us ten itself.

64
00:03:17,040 --> 00:03:18,720
So that is also a possibility.

65
00:03:18,720 --> 00:03:21,540
So one of the linked lists could just be null or empty.

66
00:03:21,540 --> 00:03:23,430
So we have understood the question.

67
00:03:23,430 --> 00:03:26,910
And we have looked at a few test cases together.

68
00:03:26,910 --> 00:03:28,110
In the next video.

69
00:03:28,110 --> 00:03:30,960
Let's try to think how we can solve this question.
