-
Notifications
You must be signed in to change notification settings - Fork 118
/
82.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>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* deleteDuplicates(struct ListNode* head) {
if (head == NULL || head->next == NULL) return head;
struct ListNode *dummy = malloc(sizeof(struct ListNode));
dummy->val = 0;
dummy->next = head;
struct ListNode *pre = dummy;
struct ListNode *p = dummy->next;
struct ListNode *q = dummy->next->next;
while (p && q) {
if (p->val == q->val) {
pre->next = q->next;
}
else if (pre->next != q) {
pre = pre->next;
}
p = p->next;
q = q->next;
}
struct ListNode *new_head = dummy->next;
free(dummy);
return new_head;
}
struct ListNode * buildList(int *nums, int numsSize) {
if (numsSize == 0) return NULL;
struct ListNode *dummy = malloc(sizeof(struct ListNode));
dummy->val = 0;
struct ListNode *p = dummy;
for (int i = 0; i < numsSize; i++) {
p->next = malloc(sizeof(struct ListNode));
p->next->val = nums[i];
p = p->next;
}
p->next = NULL;
p = dummy->next;
free(dummy);
return p;
}
int main() {
int nums[] = { 1, 2, 2, 2, 3, 3 };
struct ListNode *head = buildList(nums, sizeof(nums) / sizeof(nums[0]));
struct ListNode *p = head;
while (p) {
printf("%d->", p->val);
p = p->next;
}
printf("NIL\n");
struct ListNode *new_head = deleteDuplicates(head);
p = new_head;
while (p) {
printf("%d->", p->val);
p = p->next;
}
printf("NIL\n");
return 0;
}