删除有序链表中重复的节点(单链表)

void List::YXDeleteCF()
{
	Node *cur,*pre,*nex;
	if(head==NULL)//表示空链表
		return ;
	if(head->next==NULL)//只有一个节点
		return ;
	pre=new Node();//新申请的一个节点当作头节点,最后给delete掉
	pre->next=head;
	head=pre;
	cur=pre->next;
	nex=cur->next;
	while(nex)
	{
		while(nex && cur->data==nex->data)
			nex=nex->next;
		if(cur->next!=nex)
		{
			 while(cur!=nex)
			 {
				 pre->next=cur->next;
				 delete cur;
				 cur=pre->next;
			 }
		
			 if(cur==nex && nex!=NULL)
				 nex=nex->next;
			
		}
		else
		{
			pre=pre->next;//cur
			cur=cur->next;
			nex=nex->next;
		}
	}
	Node *p=head;
	head=head->next;
	delete p;
}

猜你喜欢

转载自blog.csdn.net/Henry313/article/details/88614125