Remove duplicate items from an unordered linked list, sort and remove
Problem
Remove duplicate items from an unordered linked list?
Appendix,
If temporary cache is not allowed, how can you solve this problem?
Analysis
If you can use extra storage space, we will open an array to save the appearance of an element. In this case, the best solution is to use a hash table, of course, but it is quite uncomfortable that the C ++ standard does not contain a hash table (in java ). Some people use hash_map under ext on the Internet, but it is not in the C ++ standard after all. It is strange to use it, if it is difficult to change the environment, it will not be able to run (like Linux and Windows ). Therefore, we can simulate it with an array. However, you should pay attention to the element boundary. For example, the int type variable is stored in the linked list. If there is a negative value, this method will not work. If the maximum value of an element is very large, the array must be large, and most of the space in the array cannot be used, resulting in a large waste of space.
In short, it is reliable to use a hash table if it can be used.
The following code traverses the linked list. If an element already exists in the array, delete it. Time complexity 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; } }}
If temporary cache is not allowed (that is, no additional storage space can be used), two pointers are required. When the first pointer points to an element, the second pointer deletes the same elements after the element. The time complexity is O (n2). The Code is as follows:
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; }}
Complete example:
#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;}