问题
从一个未排序的链表中移除重复的项?
附,
如果不允许使用临时的缓存,你如何解决这个问题?
分析
如果可以使用额外的存储空间,我们就开一个数组来保存一个元素的出现情况。 对于这种情况,最好的解决方法当然是使用哈希表,但令人非常不爽的是C++标准里是没有 哈希表的(java里有)。网上有人用ext下的hash_map,但毕竟不是C++标准里的, 用起来怪怪的,搞不好换个环境就跑不起来了(像Linux和Windows下使用就不一样)。 所以,一般用一个数组模拟一下就好了。但,这里要注意一个问题, 就是元素的边界,比如链表里存储的是int型变量。那么,如果有负值,这个方法就不奏效 了。而如果元素里的最大值非常大,那么这个数组也要开得很大,而数组中大部分空间是用 不上的,会造成空间的大量浪费。
简言之,如果可以用哈希表,还是用哈希表靠谱。
如下代码遍历一遍链表即可,如果某个元素在数组里记录的是已经出现过, 那么将该元素删除。时间复杂度O(n):
void removedulicate(node *head){
if(head==NULL) return;
node *p=head, *q=head->next;
hash[head->data] = true;
while(q){
if(hash[q->data]){
node *t = q;
p->next = q->next;
q = p->next;
delete t;
}
else{
hash[q->data] = true;
p = q; q = q->next;
}
}
}
如果不允许使用临时的缓存(即不能使用额外的存储空间),那需要两个指针, 当第一个指针指向某个元素时,第二个指针把该元素后面与它相同的元素删除, 时间复杂度O(n
2
),代码如下:
void removedulicate1(node *head){
if(head==NULL) return;
node *p, *q, *c=head;
while(c){
p=c; q=c->next;
int d = c->data;
while(q){
if(q->data==d){
node *t = q;
p->next = q->next;
q = p->next;
delete t;
}
else{
p = q; q = q->next;
}
}
c = c->next;
}
}
完整示例:
#include <iostream>
#include <cstring>
using namespace std;
typedef struct node{
int data;
node *next;
}node;
bool hash[100];
node* init(int a[], int n){
node *head, *p;
for(int i=0; i < n; ++i){
node *nd = new node();
nd->data = a[i];
if(i==0){
head = p = nd;
continue;
}
p->next = nd;
p = nd;
}
return head;
}
void removedulicate(node *head){
if(head==NULL) return;
node *p=head, *q=head->next;
hash[head->data] = true;
while(q){
if(hash[q->data]){
node *t = q;
p->next = q->next;
q = p->next;
delete t;
}
else{
hash[q->data] = true;
p = q; q = q->next;
}
}
}
void removedulicate1(node *head){
if(head==NULL) return;
node *p, *q, *c=head;
while(c){
p=c; q=c->next;
int d = c->data;
while(q){
if(q->data==d){
node *t = q;
p->next = q->next;
q = p->next;
delete t;
}
else{
p = q; q = q->next;
}
}
c = c->next;
}
}
void print(node *head){
while(head){
cout << head->data << " ";
head = head->next;
}
}
int main(){
int n = 10;
int a[] = {
3, 2, 1, 3, 5, 6, 2, 6, 3, 1
};
memset(hash, false, sizeof(hash));
node *head = init(a, n);
//removedulicate(head);
removedulicate1(head);
print(head);
return 0;
}