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
.
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int count=0;
ListNode *p,*q;
if(!head)return NULL;
p=head;
while(p->next){
if(p->val==p->next->val){
count++;
if(count>1){
q=p->next;
p->next=p->next->next;
delete q;
}
}else{
count=0;
p=p->next;
}
}
return head;
}
};