/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
if(pHead==NULL||pHead->next==NULL)
return pHead;
ListNode *Head = new ListNode(0);//建一个空的头指针
Head->next=pHead;
ListNode* p=Head->next;//p为向后移动的指针
ListNode* pf=Head;//pf指向关键字的前一个,方便删除重复指针
int number;//判断是否重复的关键字
while(p->next)
{
number=pf->next->val;
p=p->next;
if(p->val==number&&p!=NULL)
{
while(p->next!=NULL&&p->next->val==number)
{
p=p->next;
}
pf->next=p->next;
if(p->next!=NULL)
p=p->next;
}
else{
pf=pf->next;
}
}
return Head->next;
}
};
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
原文:https://www.cnblogs.com/yuanch2019/p/11583413.html