**Add Linked Lists - Amazon Top Interview Questions**

### Problem Statement :

Given a singly linked list l0 and another linked list l1, each representing a number with least significant digits first, return the summed linked list. Note: Each value in the linked list is guaranteed to be between 0 and 9. Constraints n ≤ 100,000 where n is the number of nodes in l0 m ≤ 100,000 where m is the number of nodes in l1 Example 1 Input l0 = [6, 4] l1 = [4, 7] Output [0, 2, 1] Explanation This is 46 + 74 = 120

### Solution :

` ````
Solution in C++ :
/**
* class LLNode {
* public:
* int val;
* LLNode *next;
* };
*/
LLNode* solve(LLNode* l0, LLNode* l1) {
int carry = 0;
LLNode *head = new LLNode(0), *res = head;
// now normal addition :D
// add the numbers, then send over the carry
while (l0 or l1 or carry) {
res->next = new LLNode();
res = res->next;
int a, b;
if (!l0)
a = 0;
else
a = l0->val, l0 = l0->next;
if (!l1)
b = 0;
else
b = l1->val, l1 = l1->next;
// now just add these two to each other!
int digit = (a + b + carry) % 10;
carry = (a + b + carry) / 10;
res->val = digit;
// cout << " added " << digit << " to the list " << endl;
}
return head->next;
}
```

` ````
Solution in Python :
# class LLNode:
# def __init__(self, val, next=None):
# self.val = val
# self.next = next
class Solution:
def solve(self, l0, l1):
def finish_remaining(l, c):
s, head, tail = 0, None, None
while l is not None: # Need to do extra addition only if there is a leftover carry
entry = l.val + c
s = entry % 10
c = entry // 10
l.val = s
if head is None:
head = tail = l
else:
tail.next = l
tail = l
prev, l = l, l.next
if c == 1: # Try l =[9,9] and c=1 what should happen?
prev.next = LLNode(1)
return head
if l0 is None and l1 is not None:
return l1
elif l1 is None and l0 is not None:
return l0
elif l0 is None and l1 is None:
return None
s, c, prev, head = 0, 0, None, l0
while l0 is not None and l1 is not None:
entry = l0.val + l1.val + c
s, c = entry % 10, entry // 10
l0.val = s
prev, l0, l1 = l0, l0.next, l1.next
prev.next = None
if l0 is None and l1 is not None:
prev.next = finish_remaining(l1, c)
elif l1 is None and l0 is not None:
prev.next = finish_remaining(l0, c)
elif c == 1: # l1 and l0 are both None But there is a leftover carry. Try for Example 1
prev.next = LLNode(1)
return head
```

## View More Similar Problems

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →## Tree: Level Order Traversal

Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F

View Solution →## Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <

View Solution →