2014-03-18 02:16

题目:给定一个未排序的单链表,去除其中的重复元素。

解法1:不花额外空间,使用O(n^2)的比较方法来找出重复元素。

代码:

 1 // 2.1 Remove duplicates from a linked list
 2 // inefficient without hashing space
 3 #include <cstdio>
 4 #include <unordered_set>
 5 using namespace std;
 6 
 7 struct ListNode {
 8     int val;
 9     struct ListNode *next;
10     ListNode(int x): val(x), next(nullptr) {};
11 };
12 
13 class Solution {
14 public:
15     void removeDuplicates(ListNode *head) {
16         if (head == nullptr) {
17             return;
18         }
19         
20         struct ListNode *ptr, *del, *tmp;
21         
22         ptr = head;
23         while (ptr != nullptr && ptr->next != nullptr) {
24             del = ptr;
25             while (del->next != nullptr) {
26                 if (ptr->val == del->next->val) {
27                     tmp = del->next;
28                     del->next = tmp->next;
29                     delete tmp;
30                 } else {
31                     del = del->next;
32                 }
33             }
34             ptr = ptr->next;
35         }
36     }
37 };
38 
39 int main()
40 {
41     int i;
42     int n;
43     int val;
44     struct ListNode *head, *ptr;
45     Solution sol;
46     
47     while (scanf("%d", &n) == 1 && n > 0) {
48         // create a linked list
49         ptr = head = nullptr;
50         for (i = 0; i < n; ++i) {
51             scanf("%d", &val);
52             if (head == nullptr) {
53                 head = ptr = new ListNode(val);
54             } else {
55                 ptr->next = new ListNode(val);
56                 ptr = ptr->next;
57             }
58         }
59         
60         // remove duplicates from the list
61         sol.removeDuplicates(head);
62         
63         // print the list
64         printf("%d", head->val);
65         ptr = head->next;
66         while (ptr != nullptr) {
67             printf("->%d", ptr->val);
68             ptr = ptr->next;
69         }
70         printf("\n");
71         
72         // delete the list
73         while (head != nullptr) {
74             ptr = head->next;
75             delete head;
76             head = ptr;
77         }
78     }
79     
80     return 0;
81 }

解法2:使用额外空间的话,可以用unordered_set作为hash工具,进行重复元素的查找,效率更高。

代码:

 1 // 2.1 Remove duplicates from a linked list
 2 // efficient with hashing space
 3 #include <cstdio>
 4 #include <unordered_set>
 5 using namespace std;
 6 
 7 struct ListNode {
 8     int val;
 9     struct ListNode *next;
10     ListNode(int x): val(x), next(nullptr) {};
11 };
12 
13 class Solution {
14 public:
15     void removeDuplicates(ListNode *head) {
16         if (head == nullptr) {
17             return;
18         }
19         
20         unordered_set<int> us;
21         struct ListNode *ptr, *del;
22         
23         us.insert(head->val);
24         ptr = head;
25         while (ptr->next != nullptr) {
26             if (us.find(ptr->next->val) != us.end()) {
27                 // duplicate value
28                 del = ptr->next;
29                 ptr->next = del->next;
30                 delete del;
31             } else {
32                 ptr = ptr->next;
33                 us.insert(ptr->val);
34             }
35         }
36         
37         us.clear();
38     }
39 };
40 
41 int main()
42 {
43     int i;
44     int n;
45     int val;
46     struct ListNode *head, *ptr;
47     Solution sol;
48     
49     while (scanf("%d", &n) == 1 && n > 0) {
50         // create a linked list
51         ptr = head = nullptr;
52         for (i = 0; i < n; ++i) {
53             scanf("%d", &val);
54             if (head == nullptr) {
55                 head = ptr = new ListNode(val);
56             } else {
57                 ptr->next = new ListNode(val);
58                 ptr = ptr->next;
59             }
60         }
61         
62         // remove duplicates from the list
63         sol.removeDuplicates(head);
64         
65         // print the list
66         printf("%d", head->val);
67         ptr = head->next;
68         while (ptr != nullptr) {
69             printf("->%d", ptr->val);
70             ptr = ptr->next;
71         }
72         printf("\n");
73         
74         // delete the list
75         while (head != nullptr) {
76             ptr = head->next;
77             delete head;
78             head = ptr;
79         }
80     }
81     
82     return 0;
83 }

 

 posted on 2014-03-18 02:20  zhuli19901106  阅读(514)  评论(0编辑  收藏  举报