# Add two numbers represented by linked lists | Set 1

• Difficulty Level : Easy
• Last Updated : 05 Jul, 2022

Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers.

Example:

Input:
List1: 5->6->3 // represents number 563
List2: 8->4->2 // represents number 842
Output:
Resultant list: 1->4->0->5 // represents number 1405
Explanation: 563 + 842 = 1405

Input:
List1: 7->5->9->4->6 // represents number 75946
List2: 8->4 // represents number 84
Output:
Resultant list: 7->6->0->3->0// represents number 76030
Explanation: 75946+84=76030

Approach: Traverse both lists to the end and add preceding zeros in the list with lesser digits. Then call a recursive function on the start nodes of both lists which calls itself for the next nodes of both lists till it gets to the end. This function creates a node for the sum of the current digits and returns the carry.

The steps are:

1. Traverse the two linked lists in order to add preceding zeros in case a list is having lesser digits than the other one.
2. Start from the head node of both lists and call a recursive function for the next nodes.
3. Continue it till the end of the lists.
4. Creates a node for current digits sum and returns the carry.

Below is the implementation of this approach.

## C#

Output

First List is 7 5 9 4 6
Second List is 8 4
Resultant list is 7 6 0 3 0

Complexity Analysis:

• Time Complexity: O(m + n), where m and n are numbers of nodes in first and second lists respectively.
The lists need to be traversed only once.
• Space Complexity: O(m + n).
A temporary linked list is needed to store the output number

Method 2(Using STL): Using the stack data structure

#### Approach :

• Create 3 stacks namely s1,s2,s3.
• Fill s1 with Nodes of list1 and fill s2 with nodes of list2.
• Fill s3 by creating new nodes and setting the data of new nodes to the sum of s1.top(), s2.top() and carry until list1 and list2 are empty .
• If the sum >9
• set carry 1
• else
• set carry 0
• Create a Node(say prev) that will contain the head of the sum List.
• Link all the elements of s3 from top to bottom
• return prev

Code:

## C#

Output

First List : 7 -> 5 -> 9 -> 4 -> 6
Second List : 8 -> 4
Sum List : 7 -> 6 -> 0 -> 3 -> 0

Time Complextiy: O(n)

Here n is length of the larger list

Auxiliary Space: O(n)

Extra space is used in storing the elements in the stack.

Another Approach with time complexity O(N):

The given approach works as following steps:

1. First, we calculate the sizes of both the linked lists, size1 and size2, respectively.
2. Then we traverse the bigger linked list, if any, and decrement till the size of both become the same.
3. Now we traverse both linked lists till the end.
4. Now the backtracking occurs while performing addition.
5. Finally, the head node is returned to the linked list containing the answer.

## Java

Output

First List : 7 -> 5 -> 9 -> 4 -> 6
Second List : 8 -> 4
Sum List : 7 -> 6 -> 0 -> 3 -> 0

Related Article: Add two numbers represented by linked lists | Set 2

Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.

Another Approach with easy to understand code (without appending zeros)-

In this approach we simulate how in reality we add two numbers. In the code we have taken 9->8->7 and 1->2->3 as two numbers to add. What we do is reverse these two lists to get 7->8->9 and 3->2->1 and start from the head of the lists to add numbers of individual nodes like we would in practice if we add two numbers.

For example,  first we add 7 and 3 to get 10, which means carry = 1 and value of new node will be 0. Now we continue this till the end of the list.

Steps-

1. Reverse the two number lists.
2. Simulate addition on nodes one by one. Append each node before the already calculated sum nodes.( You will better understand this step in code)
3. In the end we will get the final answer and we can return the head node.

## C#

Output

Sum is : 1 1 1 0

Complexity Analysis:

Time Complexity: O(max(m,n)), where m and n are numbers of nodes in first and second lists respectively.
The lists need to be traversed only once.
Space Complexity: O(1)
As constant extra space is used.

My Personal Notes arrow_drop_up
Recommended Articles
Page :