Problem:
You have two numbers represented by a linked list, where each node contains a single digit The digits are stored in reverse order, such that the 1’s digit is at the head of
the list Write a function that adds the two numbers and returns the sum as a linked
list
EXAMPLE
Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
Solution:
Code:
You have two numbers represented by a linked list, where each node contains a single digit The digits are stored in reverse order, such that the 1’s digit is at the head of
the list Write a function that adds the two numbers and returns the sum as a linked
list
EXAMPLE
Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
Output: 8 -> 0 -> 8
Solution:
- Have pointer on each head of the two list
- Add the data of two nodes along with carry.
- create new result node and put the value, SUM % 10
- assign SUM / 10 to carry
- Repeat until any one list exhausts.
- Then add the carry to the node, repeat till end of list
Code:
