#include <stdio.h>
#include <algorithm>
struct ListNode
{
int val;
struct ListNode *next;
};
typedef struct ListNode LNode;
typedef struct ListNode *LNode_Pointer;
struct ListNode* deleteDuplicates(struct ListNode* head)
{
LNode_Pointer p,q,q_prior,tail,temp,head_temp;
p=head;
while(p!=NULL)
{
q_prior=p;
q=p->next;
while(q!=NULL)
{
if(p->val==q->val)
{
q_prior->next=q->next;
q=q->next;
}
else
{
q_prior=q;
q=q->next;
}
}
p=p->next;
}
return head;
}
void Tail_Insert_List(LNode_Pointer &head,LNode_Pointer &tail,int val)//&tail。不加&的话每次的tail的值还是head的值
{
LNode_Pointer q=(LNode_Pointer)malloc(sizeof(LNode));
if(tail!=NULL)
{
q->val=val;
tail->next=q;
tail=tail->next;
tail->next=NULL;
}
else
{
q->val=val;
tail=q;
tail->next=NULL;
head=tail;//注意这里。不这样的话主函数的head仍然是0。
}
}
void printf_List(LNode_Pointer p)
{
while(p!=NULL)
{
if(p->next!=NULL)
{
printf("%d(地址=%p)->",p->val,p);
//printf("%d->",p->val,p);
p=p->next;
}
else
{
printf("%d(地址=%p)",p->val,p);
//printf("%d",p->val,p);
p=p->next;
}
}
}
int main()
{
int val;
LNode_Pointer head=(LNode_Pointer)malloc(sizeof(LNode));
head=NULL;
LNode_Pointer tail=(LNode_Pointer)malloc(sizeof(LNode));
tail=head;
while(scanf("%d",&val)!=EOF)
{
Tail_Insert_List(head,tail,val);
}
printf_List(head);
printf("\n");
LNode_Pointer head_return=deleteDuplicates(head);
printf_List(head_return);
return 0;
}
运行结果
提交结果: