Delete duplicate-value nodes from a sorted linked list HackerRank Solution

In this Delete duplicate-value nodes from a sorted linked list HackerRank solution, You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty.

Example

 refers to the first node in the list .

Remove 1 of the  data values and return  pointing to the revised list .

Function Description

Complete the removeDuplicates function in the editor below.

removeDuplicates has the following parameter:

  • SinglyLinkedListNode pointer head: a reference to the head of the list

Returns

  • SinglyLinkedListNode pointer: a reference to the head of the revised list

Input Format

The first line contains an integer , the number of test cases.

The format for each test case is as follows:

The first line contains an integer , the number of elements in the linked list.
Each of the next  lines contains an integer, the  value for each of the elements of the linked list.

Constraints

Sample Input

STDIN   Function
-----   --------
1       t = 1
5       n = 5
1       data values = 1, 2, 2, 3, 4
2
2
3
4

Sample Output

1 2 3 4 

Explanation

The initial linked list is: .

The final linked list is: .

Delete duplicate-value nodes from a sorted linked list HackerRank solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in C

SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* llist) {
    SinglyLinkedListNode* temp=llist;
    SinglyLinkedListNode* temp2=NULL;
 
    while(temp->next!=NULL){
       
        if(temp->data==temp->next->data){
            temp2=temp->next->next;
            free(temp->next);
            temp->next=temp2;
        }
        else{
            temp=temp->next;
        }
    }    
    return llist;
}

Problem Solution in C++

SinglyLinkedListNode* removeDuplicates(SinglyLinkedListNode* llist) {
SinglyLinkedListNode* prev=llist;
SinglyLinkedListNode* curr=llist->next;
SinglyLinkedListNode* temp;
while(curr!=nullptr){
if(prev->data==curr->data){
    prev->next=curr->next;
    temp=curr;
    curr=curr->next;
    free(temp);
}
else{
    prev=prev->next;
    curr=curr->next;
}
}
return llist;
}

Problem Solution in Python

def removeDuplicates(llist):
  if llist == None:
      return


  current = llist
  while(current.next):
      if current.data == current.next.data:
          current.next = current.next.next
      else:
          current = current.next


  return llist

Problem Solution in JavaScript

function removeDuplicates(llist) {
    let current = llist;
    if(llist === null)
        return;
       
    while(current !== null){
        console.log(current.data);
        if(current.next === null)
            break;
           
        if(current.data === current.next.data){
            current.next = current.next.next;
        }
        else
            current = current.next;
       
    }
   
    return llist;


}
Solve original Problem on HackerRank here. Checkout more HackerRank Problems

Leave a Comment