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:
        print(, end=" ")
        while is not None and ==
            head =

Problem solution in Java programming.

    public static Node removeDuplicates(Node head) {
        if (head == null) {
            return head;
        Set<Integer> set = new HashSet<>();
        Node curr = head;
        while ( != null) {
            if (set.contains( {
            else {
                curr =;     
        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;
              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;
     //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.

        var newList = new Node();
   = head;
        var p = newList;
            if( =={
                var n =;
            } else {
                p =;