I have written the code to remove duplicate from linked list, but I am not able to identify my mistake. Any possible suggestion or help would be appreciated.
#include <bits/stdc++.h>
using namespace std;
//Compiler version g++ 6.3.0
struct Node{
int data;
Node *next;
Node(int x){
data=x;
next=NULL;
}
};
void fun(Node *head){
Node *curr;
//Node *temp=curr->next;
//Node *prev;
Node *ptr;
for(curr=head;curr!=NULL;curr=curr->next){
for(ptr=curr;ptr!=NULL;ptr=ptr->next){
if(curr->data==ptr->data){
Node *hold=ptr->next; //to hold duplicate
ptr->next=ptr->next->next;
delete(hold);
}
//cout<<curr->data<<endl;
}
}
}
void print(Node *head){
Node *move=head;
while(move!=NULL){
cout<<move->data;
move=move->next;
}
}
int main()
{
int n,data1;
cin>>n;
cin>>data1;
Node *head=new Node(data1);
Node *temp=head;
while(n-1!=0){
int x;
cin>>x;
temp->next=new Node(x);
temp=temp->next;
n--;
}
fun(head);
print(head);
}
Is there any mistake in the fun
function or in calling the fun
function? I think this is likely, but I am not able to figure it out.