typedef int SListDataType;
//链表中的一个节点
typedef struct Node{
SListDataType value; //值
struct Node *next; //下一个节点的地址
}Node;
//单链表
typedef struct SList{
Node *first;// *head 第一个节点的地址
} SList;
用三个指针
SList* DelectDuplication(Node * phead){
if (phead == NULL){
return NULL;
}
Node * fack = (Node *)malloc(sizeof(Node));//假节点
fack->next = phead;
Node *prev = fack;//前驱
Node *p1 = phead;
Node *p2 = phead->next;
while (p2 != NULL){
if (p1->value != p2->value){//不相等,同时走
prev = p1;
p1 = p2;
p2 = p2->next;
}
else{
while (p2 != NULL&&p2->value == p1->value){//相等,p2先走
p2 = p2->value;
}
Node *cur = p1;
while (cur != p2){
Node *next = cur->next;
free(cur);
cur = next;
}
prev->next = p2;
p1 = p2;
if (p2!=NULL){
p2 = p2->next;
}
}
}
phead = fack->next;
free(fack);
return phead;
}