说明,1 充分抓住题目的隐藏的关键信息,比如时间复杂性要小,那么空间辅助性可以适当放松,另外数据范围不超过n;
2. 链表的基本结构
3.注意 times[abs(q->data)]=1;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define IMAX 100+1
int n, m;
int times[IMAX];
typedef struct node
{
int data;
node *next;
} node,*pnode;
pnode head,p,q;
void print(pnode head)
{
p=head->next;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
int main() {
head=(pnode)malloc(sizeof(node));
head->next=NULL;
q=head;
m=10;
unsigned int seed; /*申明初始化器的种子,注意是unsigned int 型的*/
memset(times,0,sizeof(times));
for(int i=0; i<m; i++)
{
p=(pnode)malloc(sizeof(node));
seed=i;
srand(seed);
p->data=rand()%m-m/2;
p->next=NULL;
q->next=p;
q=p;
}
print(head);
q=head->next;
p=q->next;
times[abs(q->data)]=1; //别忘了
for(int i=1; i<m; i++)
{
if(times[abs(p->data)]==0)
{
times[abs(p->data)]=1;
q=p;
p=p->next;
}
else
{
q->next=p->next;
free(p);
p=q->next;
}
}
print(head);
}