方法一:递归
link delSame(link head)
{/* 不变式:list{k..n-1}没有重复的元素,加入list{k-1}后,为了保持不变式
要从list{k-1..n-1}两两比较,如果有相同元素必定在开头两个*/
link pointer,temp = head;
if(head->next == NULL)
return head;
head->next = delSame(head->netx);
pointer = head->next;
while(pointer != NULL)
{
if(head->value == pointer->value)
{
temp->next =pointer->next;
free(pointer);
pointer = temp->next;
}
else
{
pointer = pointer->next;
temp = temp->next;
}
}
return head;
}
方法二:hash
建立一个hash_map,key为遍历链表中的内容,value是次数
1)从头遍历链表,若链表内容不在hash_map中,则加入并向后遍历
2)若链表内容在hash_map中(value = 1),则删除结点,继续向后遍历。