-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_1.cpp
84 lines (73 loc) · 1.29 KB
/
2_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed? */
#include <iostream>
using namespace std;
typedef struct Node
{
int data;
Node * next;
}node;
bool hashTable[100];
node * init(int arr[], int n)
{
node *head, *p;
for(int i = 0; i < n; i++)
{
node *newNode = new node();
newNode -> data = arr[i];
if(0 == i)
{
head = p = newNode;
continue;
}
p -> next = newNode;
p = newNode;
}
p -> next = NULL;
return head;
}
void removeDuplicate(node * head)
{
if(NULL == head)
return;
node * p = head;
node * q = head -> next;
hashTable[head -> data] = true;
while(q)
{
if(hashTable[q -> data])
{
node * t = q;
p -> next = q -> next;
q = p -> next;
delete t;
}
else
{
hashTable[q -> data] = true;
p = q;
q = q -> next;
}
}
}
void print(node * head)
{
while(head)
{
cout << head -> data << "->" ;
head = head -> next;
}
cout << "NULL" << endl;
}
int main()
{
int n = 10;
int arr[] = {3, 2, 1, 3, 5, 6, 2, 6, 3, 1};
memset(hashTable, false, sizeof(hashTable));
node * head = init(arr, 10);
removeDuplicate(head);
print(head);
system("pause");
return 0;
}