Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5 Output: 1->2->5
Example 2:
Input: 1->1->1->2->3 Output: 2->3
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
struct ListNode *p;
p=(struct ListNode *)malloc(sizeof(struct ListNode));
p->next=head;
head=p;
int a;
while(head->next!=NULL&&head->next->next!=NULL)
{
if(head->next->val==head->next->next->val)
{
a=head->next->val;
while(head->next!=NULL&&head->next->val==a)
head->next=head->next->next;
}
else
head=head->next;
}
return p->next;
}