Delete duplicate-value nodes from a sorted linked list Hackerrank

Delete duplicate-value nodes from a sorted linked list Hackerrank

You’re given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty.

For now do not be concerned with the memory deallocation. In common abstract data structure scenarios, deleting an element might also require deallocating the memory occupied by it. For an initial intro to the topic of dynamic memory please consult: Click Here
Input Format
You have to complete the Node* RemoveDuplicates(Node* head) method which takes one argument – the head of the sorted linked list. You should NOT read any input from stdin/console.

Output Format
Delete as few nodes as possible to ensure that no two nodes have the same data. Adjust the next pointers to ensure that the remaining nodes form a single sorted linked list. Then return the head of the sorted updated linked list. Do NOT print anything to stdout/console.

Sample Input

1 -> 1 -> 3 -> 3 -> 5 -> 6 -> NULL
NULL
Sample Output

1 -> 3 -> 5 -> 6 -> NULL
NULL
Explanation
1. 1 and 3 are repeated, and are deleted.
2. Empty list remains empty.

Solution
To remove duplicates from the linked list, we can traverse the list and check whether the current node and the next node have the same data. If they have the same data, we can delete the next node.
Problem Setter's code :
/*
  Remove all duplicate elements from a sorted linked list
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
Node* RemoveDuplicates(Node *head)
{
  // This is a "method-only" submission. 
  // You only need to complete this method. 

    Node * temp=head;
    while(temp->next!=NULL)
    {
        if(temp->data==temp->next->data)
        {
            Node * t=temp->next;
            temp->next=t->next;
            delete(t);
        }
        else
        {
            temp=temp->next;
        }
    }
    return head;
}
Node RemoveDuplicates(Node head) {
  // This is a "method-only" submission. 
  // You only need to complete this method. 
    
    if(head==null)
        return null;
    
    Node temp=head.next;
    Node prev=head;
    while(temp!=null)
        {
        
         if(prev.data==temp.data)
             {
             prev.next=temp.next;
             temp.next=null;
             temp=prev.next;
         }
        else
            {
              prev=temp;
              temp=temp.next;
        
        }
    }
    return head;
}
/*
  Remove all duplicate elements from a sorted linked list
  Node is defined as 
  struct Node
  {
     int data;
     struct Node *next;
  }
*/
Node* RemoveDuplicates(Node *head)
{
  // This is a "method-only" submission. 
  // You only need to complete this method. 
    
    Node *ptr = head,*temp=NULL,*tmp=NULL;
    
    while(ptr!=NULL && ptr->next!=NULL)
    {
        temp = ptr->next;
        ptr->next=NULL;
        
        while(temp!=NULL && ptr->data == temp->data)
        {
            tmp=temp;
            temp=temp->next;
            
            tmp->next=NULL;
            delete(tmp);
        }
        ptr->next = temp;
        ptr = temp;
    }
    
    return head;
}
"""
 Delete duplicate nodes
 head could be None as well for empty list
 Node is defined as
 
 class Node(object):
 
   def __init__(self, data=None, next_node=None):
       self.data = data
       self.next = next_node

 return back the head of the linked list in the below method.
"""

def RemoveDuplicates(head):
    node = head
    while node.next:
        if node.data == node.next.data:
            node.next = node.next.next
        else:
            node = node.next
    return head
 

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.