ListNode* deleteDuplication(ListNode* pHead)
{
if (pHead==NULL || pHead->next==NULL){return pHead;}
//为了防止开始节点就重复所以这里申请了一个节点
ListNode *Head = new ListNode(0);
Head->next = pHead;
ListNode *pre = Head;
ListNode *last = Head->next;
while (last!=NULL)
{
if(last->next!=NULL && last->val == last->next->val)
{
// 找到最后的一个相同节点
while (last->next!=NULL && last->val == last->next->val)
{
last = last->next;
}
pre->next = last->next;
last = last->next;
}
else
{
pre = pre->next;
last = last->next;
}
}
return Head->next;
}
偶遇一道算法题 就写出来
最新推荐文章于 2022-01-25 17:53:07 发布