AfterAcademy Tech
•
18 Nov 2019

Difficulty: Medium
Asked in: Amazon, Microsoft, Facebook
Problem Description: 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 it as a linked list.
For Example:
Input: (1 -> 8-> 2) + (3 -> 6 -> 5)
Output: (4-> 4-> 8)
Explanation: 281+ 563= 844.
Input: (4 -> 8-> 1) + (3 -> 6 )
Output: 7-> 4-> 2
Explanation: 184+ 63= 247
The node structure for the Linked List passed to your function will be
class Node {
int val
Node *next
Node(int v)
{
val = v
next = NULL
}
}
Possible follow-up questions to ask the interviewer:-
How do we add two numbers in real life? First, we add the digits at a unit place in both numbers and if it is greater than 10, we take a carry as 1 and place the unit digit of the sum in the unit place of the answer. We continue this process for the remaining places.
Solution idea
We will do the same if the numbers are in the form of a linked list. The idea is to traverse both linked lists and pick the nodes of both lists one by one and add them. We need to implement the concept of carrying too in our code.
We need to consider some cases that can happen during the iteration
Solution Steps
4. Now, there might be a case that one linked list is not fully iterated so, again iterate through that list and add carry and node's value and generate sum and append this new node of the sum to curr.next and move curr to new node.
5. There might be a condition that both linked list are fully iterated but there is carry generated, so append this carry at the end of linked list
6. Return head.
Pseudo-Code
Node addTwoNumberInLists(Node A, Node B)
{
Node head = NULL, curr = NULL
int carry = 0
while( (A!= NULL) && (B!= NULL) )
{
int s = A.val + B.val + carry
carry = s / 10 // Ten's place, becomes 0 if s < 10
s = s % 10 // Unit's place
if( head == NULL )
{
head = Node(s)
curr = head
}
else
{
curr.next = Node(s)
curr = curr.next
}
A = A.next
B = B.next
}
while ( A != NULL )
{
int s = A.val + carry
carry = s / 10
s = s % 10
curr.next = Node(s)
curr = curr.next
A = A.next
}
while( B != NULL )
{
int s = B.val + carry
carry = s / 10
s = s % 10
curr.next = Node(s)
curr = curr.next
B = B.next
}
if( carry != 0 )
curr.next = Node(carry)
return head
}
Complexity Analysis
Time Complexity: O(M+N), where M and N are the lengths of two linked lists.
Space Complexity: O(1)
Critical ideas to think!
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
May the code be with You!
Enjoy Algorithms!
AfterAcademy Tech
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.

AfterAcademy Tech
Given two integer arrays A[] and B[] of size m and n, respectively. We need to find the intersection of these two arrays. The intersection of two arrays is a list of distinct numbers which are present in both the arrays. The numbers in the intersection can be in any order.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon, Microsoft. The problem is to merge two binary trees. The overlapping nodes are replaced by the sum of corresponding nodes. We are discussing two ways of solving this.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon and Microsoft. Given a Linked List you have to swap nodes of the list in pairs.
