C++ 代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
if(!head||!head->next){
return head;
}
ListNode* dummy=new ListNode(0);
dummy->next=head;
ListNode*pre=dummy;
ListNode*cur=head;
int num=0;
while(cur){
num=0;
//1-1-1-1-2
while(cur->next&&cur->next->val==cur->val){
cur=cur->next;
num++;
}
if(num>0){
//说明有重复的
pre->next=cur->next;
}else{
//等于0,说明没有重复的
pre=cur;
}
cur=cur->next;
}
return dummy->next;
}
};