diff --git a/082_remove_duplicates_from_sorted_list_ii/Makefile b/082_remove_duplicates_from_sorted_list_ii/Makefile new file mode 100644 index 0000000..5cf5932 --- /dev/null +++ b/082_remove_duplicates_from_sorted_list_ii/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test rm_dup.c diff --git a/082_remove_duplicates_from_sorted_list_ii/rm_dup.c b/082_remove_duplicates_from_sorted_list_ii/rm_dup.c new file mode 100644 index 0000000..c451d79 --- /dev/null +++ b/082_remove_duplicates_from_sorted_list_ii/rm_dup.c @@ -0,0 +1,54 @@ +#include +#include + +struct ListNode { + int val; + struct ListNode *next; +}; + +struct ListNode* deleteDuplicates(struct ListNode* head) { + struct ListNode dummy; + struct ListNode *p, *next, *prev; + prev = &dummy; + dummy.next = head; + p = next = head; + while (p != NULL) { + while (next != NULL && next->val == p->val) { + next = next->next; + } + if (p->next == next) { + prev = p; + } else { + prev->next = next; + } + p = next; + } + return dummy.next; +} + +int main(int argc, char **argv) +{ + int i; + struct ListNode *head = NULL; + struct ListNode *prev = NULL; + struct ListNode *p; + for (i = 0; i < argc - 1; i++) { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 1]); + p->next = NULL; + if (head == NULL) { + head = p; + prev = head; + } else { + prev->next = p; + prev = p; + } + } + p = deleteDuplicates(head); + while (p != NULL) { + printf("%d ", p->val); + p = p->next; + } + printf("\n"); + return 0; +} diff --git a/083_remove_duplicates_from_sorted_list/Makefile b/083_remove_duplicates_from_sorted_list/Makefile new file mode 100644 index 0000000..5cf5932 --- /dev/null +++ b/083_remove_duplicates_from_sorted_list/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test rm_dup.c diff --git a/083_remove_duplicates_from_sorted_list/rm_dup.c b/083_remove_duplicates_from_sorted_list/rm_dup.c new file mode 100644 index 0000000..d5de716 --- /dev/null +++ b/083_remove_duplicates_from_sorted_list/rm_dup.c @@ -0,0 +1,47 @@ +#include +#include + +struct ListNode { + int val; + struct ListNode *next; +}; + +struct ListNode* deleteDuplicates(struct ListNode* head) { + struct ListNode *p, *next; + p = next = head; + while (p != NULL) { + while (next != NULL && next->val == p->val) { + next = next->next; + } + p->next = next; + p = next; + } + return head; +} + +int main(int argc, char **argv) +{ + int i; + struct ListNode *head = NULL; + struct ListNode *prev = NULL; + struct ListNode *p; + for (i = 0; i < argc - 1; i++) { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 1]); + p->next = NULL; + if (head == NULL) { + head = p; + prev = head; + } else { + prev->next = p; + prev = p; + } + } + p = deleteDuplicates(head); + while (p != NULL) { + printf("%d ", p->val); + p = p->next; + } + printf("\n"); + return 0; +}