Let’s discuss a problem add two numbers as lists and their java code.
Given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. We have to write a code to add these two numbers and return the sum as a linked list.
For this problem, you may assume the two numbers do not contain any leading zero, except the number 0 itself.
For example –
Linked List 1 : 2 -> 4 -> 6 -> NULL
Linked List 2 : 5 -> 6 -> 4 -> NULL
Result : 7 -> 0 -> 1 -> 1 -> NULL
Explanation :
In this example first, we added 2 and 5 the result is 7. Then, 6 and 4 the result is 10, so we put 0 and take 1 as a carry. Then, we added 6, 4, and 1 (carry) the result is 11, so we put 1 and take 1 as a carry.