您的位置:首页 > 其它

Remove duplicates from sorted list II

2013-04-26 10:33 162 查看
题目比较简单,就是用两个指针每次标识一个固定值的头和尾,如果只有一个则添加否则全删除。

ln* deleteDuplicates(ln* head)
{
if (!head || !head->next)
return head;

ln* pHead=NULL;
ln* pTail=pHead;
ln* pPre=head;

while(pPre)
{
ln* pNext=pPre;
while(pNext&&pNext->val==pPre->val)
pNext=pNext->next;
if (pPre->next==pNext)
{
if (pHead==NULL)
{
pHead=pTail=pPre;
pPre=pPre->next;
}
else
{
pTail->next=pPre;
pTail=pTail->next;
pPre=pPre->next;
}
}
else
{
while(pPre!=pNext)
{
ln* tmp=pPre->next;
delete pPre;
pPre=tmp;
}
pPre=pNext;
}
}
if (pTail)
pTail->next=NULL;
return pHead;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: