Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head)
{
if(head==NULL)
return NULL; if(head!=NULL&&head->next==NULL)
return head; if(head->next->next==NULL)
{
if(head->val==head->next->val)
{
head->next==NULL;
return head->next;
} if(head->val!=head->next->val)
{
return head;
}
} struct ListNode* p;
p=head; int count=;
while(p!=NULL)
{
p=p->next;
count++;
} int *array;
array=(int *)malloc(count*sizeof(int)); p=head;
int i=;
while(p!=NULL)
{
array[i]=p->val;
i++;
p=p->next;
} p=head;
for(i=;i<count-;i++)
{
if(array[i]!=array[i+])
{
p->val= array[i];
p=p->next;
}
} p->val=array[count-];
p->next=NULL; return head;
}