Yahoo Interview Question
Software Engineer / Developerswrong .concept is right .Because you r sending current node that has to be remove.node->data = node.next.data means y u copy the data part only but node.next=node.next.next have no meaning .check it
Sort the list using merge sort and then remove duplicates easily by traversing
Time Complexity : O(nlogn)
Space Complexity : None
If you are using recursive merge-sort version, the space complexity is not constant (you are using stack space). If you know an in-place merge-sort algorithm for lists, please give us the reference. :)
public static void removeDuplicates(Node head) {
if (head == null) {
return;
}
Node current = head;
while (current != null) {
Node fast = current;
while (fast.next != null) {
if (current.data == fast.next.data) {
fast.next = fast.next.next;
}
else {
fast = fast.next;
}
}
current = current.next;
}
}
public void removeduplicates(LinkNode head){
- Anonymous September 13, 2010HashTable hash = new HashTable();
LinkNode currentnode = head;
while(currentnode !=null){
if(hash.get(currentnode.data)==null){
hash.put(currentnode.data)
currentnode = currentnode.next;
}
else{
remove(currentnode);
}
}
}
public void removenode(LinkNode node){
node.next = node.next.next;
node.data= node.next.data;
}