/* struct ListNode {int val;
struct ListNode *next;
ListNode (int x): Val (x), Next (NULL) {}}; * *: Class Solution {public:listnode* deleteduplication (listnode* phead) {ListNode *dummy=new ListNode (-1);
Set an extra head node ListNode *cur=phead, *pre=dummy;
dummy->next=phead;
while (Cur&&cur->next) {if (cur->val==cur->next->val)//If there is a duplicate node {
int val=cur->val;
while (Cur&&cur->val==val) cur=cur->next;
pre->next=cur;//the previous node to the next node of the repeating node} else {pre=cur;
cur=cur->next;
} return dummy->next;
}
};
My Code: Class Solution {public:listnode* deleteduplication (listnode* phead) {if (phead==null) return NULL;
if (Null==phead->next) return phead; ListNode *ans=phead, *NXT, *pre=null;//pre before a repeating node, NXT with the current nodeDuplicate next node while (Phead) {nxt=phead;
BOOL Flag=false; while (Nxt->next&&nxt->next->val==phead->val)//Find duplicate last node {Nxt=nxt->ne
xt
Flag=true;
} if (flag)//If there is a duplicate node {if (Null==pre) ans=nxt->next;
else pre->next=nxt->next;
else Pre=phead;
phead=nxt->next;
return ans; }
};