Given two numbers represented by two lists, write a function that returns sum list. The sum list is list representation of addition of two input numbers.
Input: First List: 5->6->3 // represents number 365 Second List: 8->4->2 // represents number 248 Output Resultant list: 3->1->6 // represents number 613
Input: First List: 7->5->9->4->6 // represents number 64957 Second List: 8->4 // represents number 48 Output Resultant list: 5->0->0->5->6 // represents number 65005
Traverse both lists. One by one pick nodes of both lists and add the values. If sum is more than 10 then make carry as 1 and reduce sum. If one list has more elements than the other then consider remaining values of this list as 0. Following is the implementation of this approach.
First List is 7 5 9 4 6 Second List is 8 4 Resultant list is 5 0 0 5 6
Time Complexity: O(m + n) where m and n are number of nodes in first and second lists respectively.
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.
- Add two numbers represented by linked lists | Set 2
- Multiply two numbers represented by Linked Lists
- Subtract Two Numbers represented as Linked Lists
- Multiply two numbers represented as linked lists into a third list
- Compare two strings represented as linked lists
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Add 1 to a number represented as linked list
- Multiply Large Numbers represented as Strings
- Identical Linked Lists
- Check if two Linked Lists are permutations of each other
- First common element in two linked lists
- Union and Intersection of two Linked Lists
- Merge K sorted linked lists | Set 1
- Intersection of two Sorted Linked Lists
- Merge Sort for Linked Lists
Improved By : amogha_ha