GeeksforGeeks » Interview Questions

FlipKart Interview Question for Software Engineer/Developer (Fresher) about Algorithms, Data Structu

(8 posts)
  1. Rahul
    guest
    Posted 11 months ago #

    Given The Linked List remove the all duplicates elements from it while order should be maintained i mean 3-1-2-1-2 so o/p should be 3-2-1 e.g order of linked list shouldn't be broken & we have to
    return the original linked list without duplication

    Only O(N) or strictly less then O(N^2) Solution Required.

  2. kartik
    Moderator
    Posted 11 months ago #

    1) Sort the Linked List using Merge Sort (http://geeksforgeeks.org/?p=7740).
    2) Remove duplicates from the sorted Linked List (http://geeksforgeeks.org/?p=5075)

    Time complexity of step 1 is O(nLogn) and step 2 is O(n). So overall time complexity is O(nLogn)

  3. rajcools
    Member
    Posted 11 months ago #

    @rahul if order should be maintained then output of your case shall be 3->1->2????????

  4. Akshay
    guest
    Posted 11 months ago #

    the question is ambiguous.
    The order of ouptut would depend on which of the duplicates we choose to delete.
    (Sorted)order should be maintained or what ?

  5. Rahul
    guest
    Posted 11 months ago #

    sorting doesn't work as it will change the order have a look 1-3-1-2 if u will sort it becomes 1-1-2-3 & thus order will change because 3 should be come before 2(as was in original linked list) but we will get wrong answer if order if use sort method any other O(n) time & O(1) Solution ..??

  6. Chandan
    guest
    Posted 11 months ago #

    Go through the linked list, put the node value in a set if the set does not contain it. If the set contains the value, then delete the node

  7. Neeraj
    guest
    Posted 11 months ago #

    public static void deleteDuplicate(LinkedListNode n)
    {
        Hashtable table = new Hashtable();
        LinkedListNode previous = null;
        while (n != null)
        {
            if (table.containsKey(n.value))
                previous.next = n.next;
            else
            {
                table.put(n.value, true);
                previous = n;
            }
            n = n.next;
        }
    }
    
  8. Gaurav
    guest
    Posted 5 months ago #

    Use hashtable. The Complexity will be O(n).Keep inserting and checking for collision.If it occurs delete the node.


Reply

You must log in to post.

RSS feed for this topic