GeeksforGeeks » Interview Questions
FlipKart Interview Question for Software Engineer/Developer (Fresher) about Algorithms, Data Structu
(8 posts)-
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 duplicationOnly O(N) or strictly less then O(N^2) Solution Required.
-
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)
-
@rahul if order should be maintained then output of your case shall be 3->1->2????????
-
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 ? -
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 ..??
-
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
-
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; } } -
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.