c++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL)
return head;
ListNode* term = head;
ListNode* term2 = term;
int pre = term->val;
int tag=0;
int first = 0;
while(term->next!=NULL)
{
if(term->next->val == pre)
{
ListNode* temp = term->next->next;
term->next = temp;
tag=1;
if(first==0)
first=1;
continue;
}
int tag2=0;
if(tag==1)
{
if(first==1)
{
head = term2->next;
term2 = head;
first=0;
tag2=1;
}
else
{
term2->next = term->next;
}
}
if(tag!=1)
term2 = term;
tag=0;
if(tag2==0)
first+=2;
pre = term->next->val;
term = term->next;
}
if(tag==1)
{
if(first==1)
{
head = term2->next;
}
else
{
term2->next = term->next;
}
}
return head;
}
};