Introduction
This leetcode's problem is a favorite question of coding interviews. The data structure we are going to work with is Singly LinkedList. It's super fun.
Note: We are not going to use the inbuilt LinkedList class of C# because that is a doubly linked list.
The problem
Before we start the code, we first need to understand the problem clearly and make an algorithm to work with. Once you have an algorithm then it doesn’t even matter which programming language you are using.
Let's see what information do we have to work with.
Read the problem in detail from LeetCode: Add two numbers
You are 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. Add the two numbers and return the sum as a linked list.
Example 1:
2 -> 4 -> 3
5 -> 6 -> 4
Result: 7 -> 0 -> 8
Explanation: 342 + 465 = 807, return 708
Example 2:
1 -> 2 -> 3
1 -> 2
Result: 2 -> 4 -> 3
Explanation: 321 + 021 = 342, return 243
Example 3:
5 -> 6 -> 7
5 -> 6 -> 7
Result: 0 -> 3 -> 5 -> 1
Explanation: 765 + 765 = 1530, return 0351
Note: These are non-empty lists so we don't need to add validations for empty lists.
Approach
Okay, so the problem says we need to reverse both lists to perform addition and then reverse the result. It may look complicated on the surface but it's really easy under the hood.
Technically we don't have to reverse anything. Since the last element in the example is the first element in the list. Neither we need to append zeros at empty spaces; instead, we can just skip that addition if the value of the node is null, as simple as that.
Algorithm (Follow example 3)
We need variables one to hold the sum, and the sum could be > 10 we need variables to hold carry and another to hold the remainder.
- The looping (While loop): We iterate through the list until we reach the last node of the either list.
- Iteration 1
- With every iteration we are going to shrink the lists by one element.
- With every shrinking loop there will be pointers CurrentNodeList1, CurrentNodeList2 which are the current nodes.
- Now we need to add CurrentNodeList1.value + CurrentNodeList2.Value and update the variable sum.
- Here is the condition: if the value of sum is > 10 then we have to store the remainder in newNode's value and division in carry, then add the newNode into the resultant linkedlist.
- Let's understand this. In above example we have 5 and 5 as our first nodes.
- Perform the sum of the values of the first nodes of both linkedlist.
- 5 + 5 = 10;
- We need to store the remainder in a new node.
- NewNode.Value = 10%10 = 0;
- Now add the NewNode into the new linkedlist which you were supposed to return.
- ResultantLinkList = 0 -> null;
- Calculate the carry with division and store it in a variable
- Carry = 10/10 = 1;
- Perform the sum of the values of the first nodes of both linkedlist.
- Iteration 2
- Coming for the next iteration we have to be sure that we use the carry from the last iteration which is 1.
- Carry = 1;
- Perform the sum of the values of the second nodes of both linkedlist.
- 6 + 6 = 12
- 6 + 6 = 12 but we also have to add the carry.
- Sum = 1 + 6 + 6 = 13;
- We need to store the remainder in a new node.
- NewNode.Value = 13%10 = 3;
- Now add the NewNode into the new linkedlist which you were supposed to return.
- ResultLinkList = 0-> 3 -> null;
- Calculate the carry with division and store it in a variable
- Carry = 13/10 = 1;
- Coming for the next iteration we have to be sure that we use the carry from the last iteration which is 1.
- Iteration 3, Now you got the idea, So now I am going to skip the description.
- Sum = 1 + 7 + 7 =15;
- NewNode.Value = 15%10 = 5;
- ResultLinkList = 0-> 3 -> 5-> null;
- Carry = 15/10 = 1;
- Last step
- We are out of the while loop since we have no elements left in lists. But we still have that carry left from the last iteration. Now for this just add one more node to your resultant linkedlist with a carry.
- ResultLinkList = 0-> 3 -> 5-> 1-> null;
Note: In first iteration we have to make sure we add new node as a head to the ResultantLinkedList and from next iteration it would be ResultantLinkList.next for next it would be ResultLinkList.next.next
As you can see we have a problem here. We are not going to add (.next) for every iteration.
Instead, we can use a temporary list to pass its reference to our resultant list, and then make temporary list points to current node.




Uday DodiyaPosted Aug 28, 2022, 4:16 AM
Nice One Rikam