Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
,
return 1->2->5
.
Given 1->1->1->2->3
,
return 2->3
.
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int flag=0;
if(!head)return NULL;
ListNode *p,*q,*L;
L=new ListNode(0);
q=L;
q->next=head;
p=head;
for( ; p->next ; p=p->next){
if(p->next->val==p->val){
flag=1;
continue;
}
else{
if(flag==0)
q=q->next;
else{
flag=0;
q->next=p->next;
}
}
}
if(flag==1)
q->next=p->next;
return L->next;
}
};