forked from xperzy/careerup150
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-1.cpp
54 lines (45 loc) · 921 Bytes
/
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
#include <iostream>
#include <string>
using namespace std;
class ListNode{
public:
int val;
ListNode* next;
ListNode(int v): val(v),next(NULL){}
};
void deletedup(ListNode* head){
ListNode* p=head;
ListNode* q=head;
while (p->next){
bool f = true;
while(q!=p->next){
if (p->next->val==q->val){
p->next=p->next->next;
f = false;
break;
}
q=q->next;
}
if (f) {p=p->next;}
q=head;
}
}
int main(){
ListNode* head = new ListNode(1);
ListNode* q=head;
int a[15] = {1,2,3,4,5,6,7,4,5,2,10,8,9,0,0};
//int a[15] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
for (int i=0;i<15;i++){
ListNode* p=new ListNode(a[i]);
q->next = p;
q=p;
}
deletedup(head);
if (head==NULL){cout << "Empty List" << endl; return 0;}
while (head!=NULL){
cout << head->val << " ";
head = head->next;
}
cout << endl;
return 0;
}