/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head==NULL||head->next==NULL) {
return head;
}
ListNode* phead=head;
ListNode* pphead=NULL;
bool first=true;
while(phead&&phead->next) {
if (phead->val==phead->next->val) {
ListNode* temp;
int data=phead->val;
if (pphead==NULL) {
while(phead&&phead->val==data) {
// ListNode* temp=phead;
temp=phead;
phead=phead->next;
temp->next=NULL;
// delete temp;
// temp=0;
}
head=phead;
} else {
while(phead&&phead->val==data) {
// ListNode* temp=phead;
temp=phead;
phead=phead->next;
temp->next=NULL;
// delete temp;
// temp=0;
}
pphead->next=phead;
}
if (phead==NULL&&pphead==NULL)
return NULL;
} else {
pphead=phead;
phead=phead->next;
}
}
return head;
}
};
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head==NULL||head->next==NULL) {
return head;
}
ListNode* phead=head;
ListNode* pphead=NULL;
bool first=true;
while(phead&&phead->next) {
if (phead->val==phead->next->val) {
ListNode* temp;
int data=phead->val;
if (pphead==NULL) {
while(phead&&phead->val==data) {
// ListNode* temp=phead;
temp=phead;
phead=phead->next;
temp->next=NULL;
// delete temp;
// temp=0;
}
head=phead;
} else {
while(phead&&phead->val==data) {
// ListNode* temp=phead;
temp=phead;
phead=phead->next;
temp->next=NULL;
// delete temp;
// temp=0;
}
pphead->next=phead;
}
if (phead==NULL&&pphead==NULL)
return NULL;
} else {
pphead=phead;
phead=phead->next;
}
}
return head;
}
};