昨天的代码改下就A了
class Solution {
int flag=0;
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* anchor=new ListNode(-1);
anchor->next=head; //链表锚点
ListNode* pre=anchor;
ListNode* pnext=nullptr;
while(head)
{
pnext=head->next;
while(pnext!=nullptr&&pnext->val==head->val)//存在相等情况
{
pnext=pnext->next;flag=1;
}
if(flag==1)
{head->next=pnext;
flag=0;
}
else //正常情况
{pre=head;
head=pnext;}
}
return anchor->next;
}
};