MockQuestions

Java Intermediate Level Linked Lists Mock Interview

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

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

Question 1 of 11

Remove the duplicates from the sorted singly-linked list.

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

You are given a sorted singly linked list. You need to remove the duplicates from the linked list and return its head node. The returned list must also be a sorted list.

/*Example*/

Given list- 1 -> 1 -> 2
expected output- 1 -> 2

Given list- 1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 4
expected output- 1 -> 2 -> 3 -> 4

Solution:

We will traverse the whole list. If we find a duplicate, we will delete it.
Let curr be the pointer to the current at which we are. It will point to the first occurrence of a number.
Let nextNode be the next node of curr.

We will compare the value of curr with the value of nextNode-
-> if the values are not equal, it means we don’t have any more duplicate corresponding to the value of curr, we will update curr to point to the next node.
-> if the values are equal, we will delete nextNode. We will delete nextNode by pointing next of curr to next of nextNode. So in the illustration below, curr is at 41 and nextNode is at 42. Their values are equal. So we will delete 42. We will point the next pointer of 41 to the next of 42 i.e. 43.

(The subscript shows the identity of nodes corresponding to the same value)

nextNode
|
0 -> 1 -> 2 -> 3 -> 41 -> 42 -> 43 -> 44 -> 5
| |
curr nextNode.next

After deleting nextNode (42)-
nextNode
|
0 -> 1 -> 2 -> 3 -> 41 -> 43 -> 44 -> 5
| |
curr nextNode.next

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 deleteDuplicates(ListNode head) {
        if (head == null) return null;
        
        ListNode curr = head;
        
        while(curr != null && curr.next != null) {
            ListNode nextNode = curr.next;
            if (nextNode.val == curr.val) {
                // delete nextNode
                curr.next = nextNode.next;
            } else {
                curr = nextNode;
            }
        }
        return head;
    }
    
}

Time complexity- O(n)
Space complexity- O(1)

Written by on May 22nd, 2021

Next Question

11 Java Intermediate Level Linked Lists Interview Questions & Answers

Below is a list of our Java Intermediate Level Linked Lists interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Remove the duplicates from the sorted singly-linked list.

  • 2. Given the head of a singly linked list, and return the reversed list.

  • 3. Where do the two linked lists intersect?

  • 4. Remove all duplicates from a sorted list.

  • 5. Can you rearrange the list in order of lesser to greater than x?

  • 6. Remove zero-sum consecutive nodes from the linked list.

  • 7. Return the head of the linked list after swapping nodes.

  • 8. What is the next greater node in the inked list?

  • 9. What is the number of connected components in the linked list?

  • 10. How do you split the linked list into parts?

  • 11. How do you rearrange the list with all odd numbers first?