Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Day 24 More Linked Lists 30 days of code solution

YASH PAL, 31 July 20249 February 2026

In this HackerRank Day 24 More Linked Lists 30 days of code problem set, we need to complete the function removeDuplicates that can delete any node in the linked list that is duplicate. and return the head of the updated list. 

Problem solution in Python 2 programming.

    def removeDuplicates(self,head):
        #Write your code here
        current = head
        while current:
            while current.next and current.data == current.next.data:
                current.next = current.next.next
            current = current.next
        return head

Problem solution in Python 3 programming.

    def removeDuplicates(self,head):
        qu = []
        if head == None:
            return 
        p = head
        qu.append(p.data)
        p = p.next
        while p is not None:
            qu.append(p.data)
            p = p.next
        qu = list(dict.fromkeys(qu))
        for item in qu:
            print(item,end=' ')

Problem solution in java programming.

    public static Node removeDuplicates(Node head) {
        Set<Integer> set = new HashSet<Integer>();
        if (head == null) return head;
        set.add(head.data);
        Node curr = head;
        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)
          {
            //Write your code here
              if(head == nullptr){
                  return head;
              }
              
              Node *currentNode = head, *lastNode = head;
              int lastVal = head->data;
              for(;;){
                  currentNode = currentNode->next;
                  if(currentNode == nullptr){
                      break;
                  }
                  
                  int currentVal = currentNode->data;
                  if(currentVal == lastVal){
                      lastNode->next = currentNode->next;
                      currentNode = lastNode;
                  } else {
                    lastVal = currentVal;
                    lastNode = currentNode;
                  }
              }             
              return head;              
          }

Problem solution in c programming.

Node* removeDuplicates(Node *head){
  //Write your code here
    if (head == NULL || head->next == NULL) {
        return head;
    }
    
    Node *prev = head;
    Node *curr = head->next;
    
    while (curr != NULL) {
        if (prev->data == curr->data) {
            prev->next = curr->next;
            free(curr);
            curr = prev->next;
        } else {
            prev = curr;
            curr = curr->next;
        }
    }
    return head;
}

Problem solution in Javascript programming.

    this.removeDuplicates=function(head){
        var prev = null;
        var current = head;
        var arr = [];
        while(current != null) {
            if(arr[arr.length - 1] != current.data) {
                arr.push(current.data);
            } else {
                prev.next = current.next;
            }
            prev = current;
            current = current.next;
        }
        console.log(arr.join(' '));
    }

30 days of code coding problems solutions Hackerrank Problems Solutions HackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes