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
Equal Stacks
ou have three stacks of cylinders where each cylinder has the same diameter, but they may vary in height. You can change the height of a stack by removing and discarding its topmost cylinder any number of times. Find the maximum possible height of the stacks such that all of the stacks are exactly the same height. This means you must remove zero or more cylinders from the top of zero or more of
View Solution →Game of Two Stacks
Alexa has two stacks of non-negative integers, stack A = [a0, a1, . . . , an-1 ] and stack B = [b0, b1, . . . , b m-1] where index 0 denotes the top of the stack. Alexa challenges Nick to play the following game: In each move, Nick can remove one integer from the top of either stack A or stack B. Nick keeps a running sum of the integers he removes from the two stacks. Nick is disqualified f
View Solution →Largest Rectangle
Skyline Real Estate Developers is planning to demolish a number of old, unoccupied buildings and construct a shopping mall in their place. Your task is to find the largest solid area in which the mall can be constructed. There are a number of buildings in a certain two-dimensional landscape. Each building has a height, given by . If you join adjacent buildings, they will form a solid rectangle
View Solution →Simple Text Editor
In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, S. You must perform Q operations of the following 4 types: 1. append(W) - Append W string to the end of S. 2 . delete( k ) - Delete the last k characters of S. 3 .print( k ) - Print the kth character of S. 4 . undo( ) - Undo the last (not previously undone) operation of type 1 or 2,
View Solution →Poisonous Plants
There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan
View Solution →AND xor OR
Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value
View Solution →