Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7fef3b8

Browse files
committed
"Remove Duplicates from Sorted List II"
1 parent f42e543 commit 7fef3b8

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
218218
| 85 | [Maximal Rectangle] | |
219219
| 84 | [Largest Rectangle in Histogram] | |
220220
| 83 | [Remove Duplicates from Sorted List] | [C](src/83.c) |
221-
| 82 | [Remove Duplicates from Sorted List II] | |
221+
| 82 | [Remove Duplicates from Sorted List II] | [C](src/82.c) |
222222
| 81 | [Search in Rotated Sorted Array II] | [C](src/81.c) |
223223
| 80 | [Remove Duplicates from Sorted Array II] | [C](src/80.c) |
224224
| 79 | [Word Search] | [C](src/79.c) |

‎src/82.c

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
9+
struct ListNode* deleteDuplicates(struct ListNode* head) {
10+
if (head == NULL || head->next == NULL) return head;
11+
12+
struct ListNode *dummy = malloc(sizeof(struct ListNode));
13+
dummy->val = 0;
14+
dummy->next = head;
15+
struct ListNode *pre = dummy;
16+
struct ListNode *p = dummy->next;
17+
struct ListNode *q = dummy->next->next;
18+
19+
while (p && q) {
20+
if (p->val == q->val) {
21+
pre->next = q->next;
22+
p = p->next;
23+
q = q->next;
24+
}
25+
else {
26+
if (pre->next == q) {
27+
p = p->next;
28+
q = q->next;
29+
}
30+
else {
31+
pre = pre->next;
32+
p = p->next;
33+
q = q->next;
34+
}
35+
}
36+
}
37+
38+
struct ListNode *new_head = dummy->next;
39+
free(dummy);
40+
41+
return new_head;
42+
}
43+
44+
struct ListNode * buildList(int *nums, int numsSize) {
45+
if (numsSize == 0) return NULL;
46+
47+
struct ListNode *dummy = malloc(sizeof(struct ListNode));
48+
dummy->val = 0;
49+
struct ListNode *p = dummy;
50+
for (int i = 0; i < numsSize; i++) {
51+
p->next = malloc(sizeof(struct ListNode));
52+
p->next->val = nums[i];
53+
p = p->next;
54+
}
55+
p->next = NULL;
56+
57+
p = dummy->next;
58+
free(dummy);
59+
60+
return p;
61+
}
62+
63+
int main() {
64+
65+
int nums[] = { 1, 2, 2, 2, 3, 3 };
66+
struct ListNode *head = buildList(nums, sizeof(nums) / sizeof(nums[0]));
67+
struct ListNode *p = head;
68+
while (p) {
69+
printf("%d->", p->val);
70+
p = p->next;
71+
}
72+
printf("NIL\n");
73+
74+
struct ListNode *new_head = deleteDuplicates(head);
75+
76+
p = new_head;
77+
while (p) {
78+
printf("%d->", p->val);
79+
p = p->next;
80+
}
81+
printf("NIL\n");
82+
83+
return 0;
84+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /