- Notifications
You must be signed in to change notification settings - Fork 117
/
Copy path82.c
76 lines (61 loc) · 1.59 KB
/
82.c
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
#include<stdio.h>
#include<stdlib.h>
structListNode {
intval;
structListNode*next;
};
structListNode*deleteDuplicates(structListNode*head) {
if (head==NULL||head->next==NULL) returnhead;
structListNode*dummy=malloc(sizeof(structListNode));
dummy->val=0;
dummy->next=head;
structListNode*pre=dummy;
structListNode*p=dummy->next;
structListNode*q=dummy->next->next;
while (p&&q) {
if (p->val==q->val) {
pre->next=q->next;
}
elseif (pre->next!=q) {
pre=pre->next;
}
p=p->next;
q=q->next;
}
structListNode*new_head=dummy->next;
free(dummy);
returnnew_head;
}
structListNode*buildList(int*nums, intnumsSize) {
if (numsSize==0) returnNULL;
structListNode*dummy=malloc(sizeof(structListNode));
dummy->val=0;
structListNode*p=dummy;
for (inti=0; i<numsSize; i++) {
p->next=malloc(sizeof(structListNode));
p->next->val=nums[i];
p=p->next;
}
p->next=NULL;
p=dummy->next;
free(dummy);
returnp;
}
intmain() {
intnums[] = { 1, 2, 2, 2, 3, 3 };
structListNode*head=buildList(nums, sizeof(nums) / sizeof(nums[0]));
structListNode*p=head;
while (p) {
printf("%d->", p->val);
p=p->next;
}
printf("NIL\n");
structListNode*new_head=deleteDuplicates(head);
p=new_head;
while (p) {
printf("%d->", p->val);
p=p->next;
}
printf("NIL\n");
return0;
}