Add Two Numbers As Linked Lists

Posted: 16 Feb, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

You are given two linked lists representing two non-negative numbers. The digits in the linked list 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. Your task is to find the sum list and return the head of the sum list where the sum list is a linked list representation of the addition of two numbers.

Note :
The number represented by the linked lists do not contain any leading zeros. 
Input Format:
The first line of input contains a single integer T, representing the number of test cases. 

Then the T test cases follow.

The first line of each test case contains the elements of the first singly linked list separated by a single space and terminated by -1.

The second line of each test case contains the elements of the second singly linked list separated by a single space and terminated by -1.
Output Format:
For each test case, print the sum linked list. The elements of the sum list should be single-space separated and terminated by -1.

The output of each test case must be printed on a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function. 
Constraints :
1 <= T <= 10
1 <= M, N <= 5 * 10^4
0 <= data[i] <= 9 and data[i] != -1

Where 'M' and 'N' are the number of nodes in the two linked lists, 'data[i]' is the data of the 'i-th' node.

Time Limit: 1 sec
Approach 1
  • A simple approach could be to recursively add the nodes of the linked list while keeping track of the carry generated.
  • The idea behind this approach is the same as finding the sum of two numbers manually on a paper, where we start by adding the LSD and move on till the MSD. Also, keeping track of the carry generated in every iteration.
  • As the linked lists represent the numbers in reverse order, the LSD occurs at the starting of the list and MSD occurs at the ending.
  • So to perform the addition, we can add the corresponding nodes (i.e. nodes present at the same position) of the two linked lists to generate the resulting digit in the sum list.
  • While adding, it is possible that the sum of the two nodes is greater than 9. This will generate a carry that needs to be added to the next significant digit.
  • In case one of the lists has more number elements than the other, then we consider the remaining values of the other list as 0.

 

The steps are as follows:

 

  • Let the recursive function be addTwoNumbersHelper('NODE1', 'NODE2', 'CARRY') which takes the current node of the first list, the current node of the second list, and the carry generated from the previous iteration/addition.
  • Initially, we call the function as addTwoNumbersHelper('HEAD1', 'HEAD2', 0).
  • Base Condition: If 'NODE1' = NULL AND 'NODE2' = NULL AND 'CARRY' = 0, return NULL.
  • Let 'VAL1' and 'VAL2' denote the value stored in the current nodes of the two linked lists, respectively. And 'NEXT_1' and 'NEXT_2' denote the pointers to the node present after the current nodes of the two linked lists, respectively.
  • If 'NODE1' = NULL, then the list 1 is empty so consider the current node’s value as zero i.e. 'VAL1' = 0, and the node after the current node as NULL i.e. 'NEXT_1' = NULL.
  • Otherwise, list 1 is not empty. So, 'VAL1' = 'NODE1'.data and 'NEXT_1' = 'NODE1'.next.
  • If 'NODE2' = NULL, then the list 2 is empty so consider the current node’s value as zero i.e. 'VAL2' = 0 and the node after the current node as NULL i.e. 'NEXT_2' = NULL. .
  • Otherwise, list 2 is not empty. So, 'VAL2' = 'NODE2'.data  and 'NEXT_2' = 'NODE2'.next.
  • Add the values in the current nodes of the two linked lists along with the carry to generate the resulting sum. Store it in a variable, say sum i.e. 'SUM' = 'VAL1' + 'VAL2' + 'CARRY'.
  • Get the next node of the sum list by creating a new node having data = 'SUM'%10. Store the new node in a variable say, 'SUM_NODE'.
  • Recursively call the function to generate the remaining nodes of the sum list as:
    • 'SUM_NODE'.next = addTwoNumbersHelper('NEXT_1', 'NEXT_2', 'SUM'/10)
  • Return 'SUM_NODE'.
Try Problem