QSN. Given an unsorted linked list of N nodes. The task is to remove duplicate elements from this unsorted Linked List. When a value appears in multiple nodes, the node which appeared first should be kept, all others duplicates are to be removed. I tried to write this code on my own but showing error i attached after this code.
class Solution
{public: Node * removeDuplicates( Node *head)
{ Node *curr = head;
Node *temp = head->next;
while(curr!=NULL)
{
temp = curr->next;
while(temp!=NULL)
{
if(curr->data == temp->data)
{
curr->next = temp->next;
temp = curr->next;
}
else
temp = temp->next;
}
curr = curr->next;
}
return head;
}
};
It is not running for some testcases given below:
For Input: 1
6
2 2 2 3 4 2
Your Output: 2
Expected Output: 2 3 4