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.


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


l0 = [6, 4]
l1 = [4, 7]


[0, 2, 1]


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;
            a = l0->val, l0 = l0->next;

        if (!l1)
            b = 0;
            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
# = 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
           = l
                    tail = l
                prev, l = l,
            if c == 1:  # Try l =[9,9] and c=1 what should happen?
       = 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,, = None
        if l0 is None and l1 is not None:
   = finish_remaining(l1, c)
        elif l1 is None and l0 is not None:
   = finish_remaining(l0, c)
        elif c == 1:  # l1 and l0 are both None But there is a leftover carry. Try for Example 1
   = LLNode(1)
        return head

View More Similar Problems

Median Updates

The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o

View Solution →

Maximum Element

You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each

View Solution →

Balanced Brackets

A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra

View Solution →

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 →