SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
Remove duplicates from a unsorted linked list
Remove duplicates from a unsorted linked list
#include
#include
#define MAX 1000000 struct node { int data; struct node *next; }; struct node* newnode(int data) { struct node *new=(struct node*)malloc(sizeof(struct node)); new->data=data; new->next=NULL; return new; } void insertashead(struct node **head,int data) { struct node *temp=newnode(data); if((*head)==NULL) *head=temp; else { temp->next=(*head); (*head)=temp; } } void printlist(struct node *head) { while(head!=NULL) { printf("%d ",head->data); head=head->next; } } void deletenode(struct node *del) { struct node *temp=del->next; del->data=temp->data; del->next=temp->next; free(temp); } void removeduplicates(struct node *head) { struct node *current=head; int hash[MAX]; memset(hash,0,sizeof(hash)); while(current!=NULL) { if(hash[current->data]==1) { deletenode(current); } else { hash[current->data]=1; current=current->next; } } } int main(void) { struct node *head=NULL; int n,x,i,key; scanf("%d",&n); for(i=0;i
PREVIOUS
NEXT
HOME