Given a sorted linked list, delete all duplicates such this each element appear only once.
For example,
Given 1->1->2 , return 1->2 .
Given 1->1->2->3->3 , return 1->2->3 .
1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * struct ListNode *next;6 * };7 */8 structlistnode* Deleteduplicates (structlistnode*head)9 {Ten if(head==NULL) One returnNULL; A - if(head!=null&&head->next==NULL) - returnhead; the - - if(head->next->next==NULL) - { + if(head->val==head->next->val) - { +head->next==NULL; A returnHead->Next; at } - - if(head->val!=head->next->val) - { - returnhead; - } in } - to + structlistnode*p; -p=head; the * intCount=0; $ while(p!=NULL)Panax Notoginseng { -P=p->Next; thecount++; + } A the + int*Array; -Array= (int*)malloc(count*sizeof(int)); $ $p=head; - intI=0; - while(p!=NULL) the { -Array[i]=p->Val;Wuyii++; theP=p->Next; - } Wu -p=head; About for(i=0; i<count-1; i++) $ { - if(array[i]!=array[i+1]) - { -P->val=Array[i]; AP=p->Next; + } the } - $p->val=array[count-1]; thep->next=NULL; the the returnhead; the}
Leecode-remove Duplicates from Sorted List