In this HackerRank Day 24: More Linked Lists problem solution A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next, pointing to another node (i.e.: the next node in a list).

A removeDuplicates function is declared in your editor, which takes a pointer to the head node of a linked list as a parameter. Complete removeDuplicates so that it deletes any duplicate nodes from the list and returns the head of the updated list.

HackerRank Day 24: More Linked Lists problem solution

Problem solution in Python programming.

    def removeDuplicates(self,head):
        if not head:
            return
        print(head.data, end=" ")
        while head.next is not None and head.data == head.next.data:
            head = head.next
        self.removeDuplicates(head.next)
  



Problem solution in Java programming.

    public static Node removeDuplicates(Node head) {
        if (head == null) {
            return head;
        }
        
        Set<Integer> set = new HashSet<>();
        Node curr = head;
        set.add(curr.data);
        while (curr.next != null) {
            if (set.contains(curr.next.data)) {
                curr.next = curr.next.next;
            } 
            else {
               set.add(curr.next.data);
                curr = curr.next;     
            }
        }
        
        return head;
    }




Problem solution in C++ programming.

          Node* removeDuplicates(Node *head) {
              if(head == nullptr) return head;
              while(head->next != nullptr && head->data == head->next->data) {
                  auto dup = head->next;
                  head->next = dup->next;
                  delete dup;
              }
              removeDuplicates(head->next);
              return head;
          }
 



Problem solution in C programming.

Node* removeDuplicates(Node *head){
  //Write your code here
    if(!head) return head;
    
  Node* currNode = head;
  Node* nextNode = currNode->next;  
  
  //move forward Node  
  while(nextNode) {
     //continue comparing pointer-1's data to matching -pointer's data
      //and moving forward until mismatch is found
     while(nextNode && currNode->data == nextNode->data) {
                  
         //delete duplicateNode
         Node* tmp = nextNode;
         //move to nextNode
         nextNode = nextNode->next;
         //delete previous node
         tmp->next = NULL;
         free(tmp);
     }
     //check if nextNode pointer moved ahead (indicating duplicate entries) 
     if(currNode->next != nextNode) {
         currNode->next = nextNode;
     }
     //move pointers forward (if not reached end)
     currNode = nextNode; 

     if(currNode) {
         nextNode = currNode->next;
     } 
  }  
    
  return head;    
}



Problem solution in Javascript programming.

    this.removeDuplicates=function(head){
        var newList = new Node();
            newList.next = head;
        var p = newList;
        
        while(p.next){
            if(p.next.data == p.data){
                var n = p.next;
                p.next = n.next;
            } else {
                p = p.next;
            }
        }
        
        return newList.next;
    }