forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
16e1c77
commit 6f9ba95
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test rm_dup.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test rm_dup.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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; | ||
} |