-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.cpp
54 lines (47 loc) · 1.16 KB
/
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
/*************************************************************************
> File Name: 1.cpp
> Author: Louis1992
> Mail: [email protected]
> Blog: http://gzc.github.io
> Created Time: Mon Nov 9 00:37:59 2015
************************************************************************/
#include<iostream>
using namespace std;
struct Linkedlist {
int v;
Linkedlist *next;
Linkedlist(int _v):v(_v){}
};
void removeDuplicate(Linkedlist *head) {
if(!head || !head->next) return;
int pre(head->v);
Linkedlist *newhead = head;
head = head->next;
while(head) {
if(head->v == pre) {
//do nothing
} else {
newhead->next = head;
newhead = head;
}
head = head->next;
}
newhead->next = nullptr;
}
void format(Linkedlist *head) {
while(head) {
cout << head-> v << " ";
head = head->next;
}
cout << endl;
}
int main() {
Linkedlist *l1 = new Linkedlist(1);
Linkedlist *l2 = new Linkedlist(1);
Linkedlist *l3 = new Linkedlist(2);
l1->next = l2;
l2->next = l3;
removeDuplicate(l1);
format(l1);
return 0;
}