/
* 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) {
ListNode* f = NULL;
ListNode* s =NULL;
ListNode* p =NULL;
bool flag = false;
if (head==NULL) return head;
if (head->next == NULL) return head;
while(head!=NULL && head->next!=NULL && head->val == head->next->val ){
f = head -> next;
while(f!=NULL&&head->val == f->val){
f = f->next;
}
head = f;
}
if (head ==NULL || head -> next== NULL) return head;
// cout<<head<<endl;
s = head->next;
p = head;
while(s!=NULL && s->next!=NULL ){
// cout<<s->val<<endl;
ListNode* s1 = s->next;
if (s->val == s1->val){
// cout<<1<<endl;
while(s->val == s1->val ){
if (s1->next!=NULL){
s1 = s1->next;
// cout<<s1->val<<endl;
}
else
{
flag = true;
break;
}
}
s->val = s1 ->val;
s ->next = s1->next;
continue;
}
s = s -> next;
}
if (flag){
s = head;
p = head->next;
if (p == NULL && s == head ) {
head->next = NULL;
}
while(p->next!=NULL){
p = p->next;
s = s->next;
}
s -> next = NULL;
}
// s = head->next;
// f = head;
// while( s != NULL ){
// if (s == head){
// f -> next=NULL;
// }
// f = f->next;
// s = s->next;
// }
return head;
}
};