class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* Head=new ListNode(-1);
Head->next=head;
ListNode *pre=Head,*cur=head;
while(cur)
{
int PreVal=cur->val;
if(cur->next&&PreVal==cur->next->val)
{
while(cur&&cur->val==PreVal)
{
pre->next=cur->next;
delete cur;
cur=pre->next;
}
}
else
{
pre=pre->next;
cur=cur->next;
}
}
return Head->next;
}
};82. Remove Duplicates from Sorted List II
最新推荐文章于 2024-01-17 14:07:10 发布
本文介绍了一种在链表中删除所有重复元素的方法。通过使用一个预头结点简化边界条件处理,该算法能有效移除相邻重复节点,直至链表中不再存在连续重复元素。
723

被折叠的 条评论
为什么被折叠?



