


You are given two non-negative numbers 'num1' and 'num2' represented in the form of linked lists.
The digits in the linked lists are stored in reverse order, i.e. starting from least significant digit (LSD) to the most significant digit (MSD), and each of their nodes contains a single digit.
Calculate the sum of the two numbers and return the head of the sum list.
Input:
'num1' : 1 -> 2 -> 3 -> NULL
'num2' : 4 -> 5 -> 6 -> NULL
Output: 5 -> 7 -> 9 -> NULL
Explanation: 'num1' represents the number 321 and 'num2' represents 654. Their sum is 975.
The first line contains a single integer 'm', the number of elements in 'num1'.
The second line contains 'm' integers, the elements of the first singly linked list / digits of 'num1'.
The third line contains a single integer 'n', the number of elements in 'num2'.
The fourth line contains 'n' integers, the elements of the second singly linked list / digits of 'num2'.
Return the sum linked list.
You do not need to print anything; it has already been taken care of. Just implement the given function.
3
1 2 3
3
4 5 6
5 7 9
'num1' represents the number 321 and 'num2' represents 654. Their sum is 975.
2
0 1
1
0
0 1
'num1' represents 10 and 'num2' represents 0. Their sum is 10.
1
2
2
9 9
1 0 1
'num1' represents 2 and 'num2' represents 99. Their sum is 101.
The expected time complexity is O('m' + 'n').
1 <= 'm', 'n' <= 5 * 10^4
0 <= 'data' in any Linked List node <= 9
The numbers do not contain any leading zeros.
If the number is zero, then there is one node having 'data' = 0.
Time Limit: 1 sec
Can you perform the addition by recursively traversing through the lists?
The steps are as follows:
O('m' + 'n'), where 'm' and 'n' are the lengths of the linked lists.
We are traversing both the linked lists completely and it takes O(L) (where L is the number of nodes in the list) time to traverse each list.
Hence the time complexity is O('m' + 'n').
O(Max('m', 'n')), where 'm' and 'n' are the lengths of the linked lists.
Extra space is required for the recursion stack and also for storing the sum list. The maximum depth of the stack and the length of the sum list is Max(Length of 'num1', Length of 'num2') + 1.
Hence the space complexity is O(Max('m', 'n')).