MockQuestions

Java Beginner Level Linked Lists Mock Interview

To help you prepare for your Java Beginner Level Linked Lists interview, here are 9 interview questions and answer examples.

Get More Information About Our Java Beginner Level Linked Lists Interview Questions

Question 1 of 9

Add two numbers from two separated singly-linked lists.

This interview question concentrates on the developer's skills working with Java linked lists and while loops.

You are given two numbers that are represented as two singly-linked lists. Each node of the linked list represents a digit in the number. The digits are stored in reverse order. That is, the unit-place digit is the head of the linked list. Your task is to find the sum of the two numbers and return the result in the form of a linked list.

/*Example*/

Let two numbers are 123 and 456
so given linked lists will be
	3 -> 2 -> 1
and	6 -> 5 -> 4

The sum of the numbers is 579
So the expected result will be
	9 -> 7 -> 5

Constraints:  The numbers of nodes in each given linked lists will be between 1 and 100

Solution:

The first solution that comes to mind is to get the value numbers from given linked lists. Then add them, and then create a linked list from the result again.

But the number of the digits in the list is up to 100, which means the range of the numbers is [1, 10100], which is too large for storing numbers directly in their bit forms in a computer.

What we can do is take digits one by one from the linked lists and add them. If it is smaller than 10, directly append it to the resultant list; if it is greater than 10, then append its unit place to the list and forward carry 1 to the next digits as we do in pen-paper while adding two numbers.

More formally,
Let, a and b are the linked lists and res be result list.
ai and bi are digits stored in the ith node.

ri = ai + bi + ci - 1 // take carry from last calculation
resi = ri % 10 // store the unit place i.e. remainder by 10
ci = floor(ri / 10) // forward carry, if any

public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) {
        this.val = val; 
        this.next = next; 
    }
}


class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode res = new ListNode(0);
        int carry = 0;
        ListNode a = l1;
        ListNode b = l2;
        ListNode currRes = res;
        
        while(a != null || b != null) {
            // if either a or b are null i.e. length of one number
            // is greater than other, then assume(extend) 0
            // at other places
            int digitA = (a != null) ? a.val : 0;
            int digitB = (b != null) ? b.val : 0;
            
            int r = digitA + digitB + carry;
            
            ListNode node = new ListNode(r % 10);
            carry = r / 10;
            currRes.next = node;
 
            currRes = currRes.next;
            a = (a != null) ? a.next : null;
            b = (b != null) ? b.next : null;
        }
        
        // if there is carry at the end. It means
        // addition of most significant digits of given numbers
        // is greater than 10. E.g a = 999, b = 999
        if (carry > 0) {
            ListNode node = new ListNode(carry);
            currRes.next = node;
        }
        
        return res.next;
    }
    
}

Time complexity - O(max(n, m)) // n and m are length of the two digits
Space complexity - O(1)

Written by on May 21st, 2021

Next Question

9 Java Beginner Level Linked Lists Interview Questions & Answers

  • 1. Add two numbers from two separated singly-linked lists.

  • 2. Return the middle node of a given linked list.

  • 3. Remove elements from the linked list that are equal to the target.

  • 4. Delete the node in a singly-linked list.

  • 5. Merge two sorted lists.

  • 6. Can you remove nodes between indices from list1 and replace them with list2 nodes?

  • 7. Create a deep copy of the special-linked list.

  • 8. Does the list contain a cycle?

  • 9. How do you rotate the list?