Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ritikamotwani/b8200c13bef28bf32d378e97a2752cf5 to your computer and use it in GitHub Desktop.
Save ritikamotwani/b8200c13bef28bf32d378e97a2752cf5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct listnode
{
int val;
struct listnode *next;
};
struct listnode *insert(struct listnode *start, int n)
{
struct listnode *tmp;
tmp = (struct listnode *)malloc(sizeof(struct listnode));
tmp->val = n;
struct listnode *p = start;
if(start == NULL)
{
start = tmp;
tmp->next = NULL;
return start;
}
while(p->next!=NULL)
{
p = p->next;
}
p->next = tmp;
tmp->next = NULL;
return start;
}
void display(struct listnode *start)
{
struct listnode *p = start;
while(p != NULL)
{
printf("%d\n",p->val);
p = p->next;
}
}
struct listnode *delet(struct listnode *start, int n)
{
struct listnode *p = start;
if(start->val == n)
{
start = start->next;
return start;
}
else
{
while(p->next->val != n)
p=p->next;
struct listnode *tmp;
tmp = p->next;
p->next = tmp->next;
free(tmp);
return start;
}
}
struct listnode *deleteDuplicates(struct listnode *start)
{
int ar[10000];
int i;
memset(ar,0,sizeof(ar));
struct listnode *p = start;
while(p != NULL)
{
if(ar[p->val] == 0)
ar[p->val]++;
else
{
start = delet(start, p->val);
}
p = p->next;
}
return start;
}
int main()
{
int n;
scanf("%d",&n);
int g = n;
struct listnode *start=NULL;
while(g--)
{
int val;
scanf("%d",&val);
start = insert(start,val);
}
display(start);
start = deleteDuplicates(start);
display(start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment