# Given two linked lists, both represent a number need to find its sum linked list.

As described in problem statement, we have two linked lists represents as decimal numbers. We aim to find its sum as a linked list. Let us understand this with an example.

We can solve this problem in two different ways.

1. Digit by digit sum calculation.
2. Converting a list to the number and calculate the sum and switch back to the linked list.

Here in this article, we only solve using the first approach, as the second approach is a very straight forward way to solve this problem.

### Digit by digit sum calculation

Here we add digits by digits in each node and we pass the carry to its respective previous nodes sum. We do this process until all digits processes. Finally we get the sum linked list. Algorithm for this approach is as follows.

#### Algorithm

1. We move both nodes till any one of them reaches to its end.
2. If both nodes at the current position are null, then the sizes of those two nodes are equal.
1. If both lists size is the same, then we recursively calculate the sum of the digits while backtracking, still if we left with a carry. We create a node with that data then prepend that node to our result list.
2. If the original list size is higher, then we move the position until it left with the second list size. We prepend the extra nodes by adding carry to it.
3. The second list size is higher then we move that until it left with original list size, do the same as in the second step.

Let us jump into the code for this approach.

``````// Sum if both lists size is equal.
// @result -- output list.
// returns carry.
{
{
return 0;
}
sum = sum + div;
Node *tmp = new Node(sum % 10);
tmp->next = *result;
*result = tmp;
return (sum / 10);
}

// Output -- carry stores in "div"
{
{
return;
}
if (*div == 0)
{
return;
}
int sum = head->data + *div;
*div = sum / 10;
}

// Gets the sum linked list.
{
Node *result = NULL;
Node *sum = NULL;
while (curr1 && curr2)
{
curr1 = curr1->next;
curr2 = curr2->next;
}
if (curr1 == NULL && curr2 == NULL)
{
if (res != 0)
{
Node *tmp = new Node(res);
tmp->next = result;
result = tmp;
}
return result;
}
if (curr1 == NULL)
{
Node *prev = sum;
while (curr2->next)
{
prev->next = new Node(tmp->data);
prev = prev->next;
tmp = tmp->next;
curr2 = curr2->next;
}
if (res != 0)
{
if (res != 0)
{
tmp = new Node(res);
tmp->next = sum;
sum = tmp;
}
}
prev->next = result;
return sum;
}
else
{
Node *prev = sum;
while (curr1->next)
{
prev->next = new Node(tmp->data);
prev = prev->next;
tmp = tmp->next;
curr1 = curr1->next;
}
if (res != 0)
{
if (res != 0)
{
tmp = new Node(res);
tmp->next = sum;
sum = tmp;
}
}
prev->next = result;
return sum;
}
}``````

The complexity of this solution is $O(N)$, where N is the length of the most extended linked list with a constant space. Let us see the complete executable code with the main function.

``````// Output
1000111``````

It is all about finding the sum linked list with given two linked lists represents as integers. Please try to solve this problem with the other approach and let our readers know by commenting here below. Happy Learning!.