Add Two Numbers

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.

Add Two Numbers as Lists

Now, the question is how we can approach this problem.

Programming questions on linked list

Linked list video tutorials

Add Two Numbers Represented by Linked Lists – Java Code

The easiest approach is to traverse both the linked lists and add the numbers. If the sum is greater than 9 then we have to take the carry and handle this case.

Add Two Numbers as Lists InterviewBit Solution

We have already discussed the logic, let’s write a code to solve this problem.

Add two numbers video tutorial

Remove Nth node from the end of a linked list

Check if a linked list is palindrome or not

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.