AcWing 29. 删除链表中重复的节点
原题链接
中等
作者:
术
,
2020-12-23 14:34:00
,
所有人可见
,
阅读 306
class Solution
{
public:
ListNode* deleteDuplication(ListNode* head)
{
//cout<<"1"<<endl;
ListNode *p=head;
if(!head)
return head;
ListNode *l3=new ListNode(-1);
l3->next=head;
ListNode *pre=l3;
while(p&&p->next)
{
ListNode *o=p->next;
if(p->val==p->next->val)
{
ListNode *r=p->next;
while(r&&p->val==r->val)
r=r->next;
pre->next=r;
//pre=pre->next;
p=r;
}
else
{
pre->next=p;
pre=p;
p=o;
}
}
if(l3->next)
return l3->next;
else
return nullptr;
}
};