From ebc3bcf15e697468b658a599eb3c651a0b4069e2 Mon Sep 17 00:00:00 2001 From: Leo Ma Date: Fri, 27 Oct 2017 09:30:27 +0800 Subject: [PATCH] Linked list cycle Signed-off-by: Leo Ma --- 141_linked_list_cycle/Makefile | 2 + 141_linked_list_cycle/list_cycle.c | 47 +++++++++++++++++++++++ 142_linked_list_cycle_ii/Makefile | 2 + 142_linked_list_cycle_ii/list_cycle.c | 55 +++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 141_linked_list_cycle/Makefile create mode 100644 141_linked_list_cycle/list_cycle.c create mode 100644 142_linked_list_cycle_ii/Makefile create mode 100644 142_linked_list_cycle_ii/list_cycle.c diff --git a/141_linked_list_cycle/Makefile b/141_linked_list_cycle/Makefile new file mode 100644 index 0000000..cf8bd0d --- /dev/null +++ b/141_linked_list_cycle/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test list_cycle.c diff --git a/141_linked_list_cycle/list_cycle.c b/141_linked_list_cycle/list_cycle.c new file mode 100644 index 0000000..1b16d87 --- /dev/null +++ b/141_linked_list_cycle/list_cycle.c @@ -0,0 +1,47 @@ +#include +#include +#include + +struct ListNode { + int val; + struct ListNode *next; +}; + +static bool hasCycle(struct ListNode *head) +{ + if (head == NULL || head->next == NULL) { + return false; + } + + bool first = true; + struct ListNode *p0, *p1; + for (p0 = head, p1 = head; p1 != NULL && p1->next != NULL; p0 = p0->next, p1 = p1->next->next) { + if (p0 == p1 && !first) { + return true; + } + first = false; + } + + return false; +} + +int main(int argc, char **argv) +{ + int i, count = argc - 1; + struct ListNode *head = NULL, *p, *prev; + for (i = 0; i < count; i++) { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 1]); + p->next = NULL; + if (head == NULL) { + head = p; + } else { + prev->next = p; + } + prev = p; + } + p->next = head; + + printf("%s\n", hasCycle(head) ? "true" : "false"); + return 0; +} diff --git a/142_linked_list_cycle_ii/Makefile b/142_linked_list_cycle_ii/Makefile new file mode 100644 index 0000000..cf8bd0d --- /dev/null +++ b/142_linked_list_cycle_ii/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test list_cycle.c diff --git a/142_linked_list_cycle_ii/list_cycle.c b/142_linked_list_cycle_ii/list_cycle.c new file mode 100644 index 0000000..d8f55bc --- /dev/null +++ b/142_linked_list_cycle_ii/list_cycle.c @@ -0,0 +1,55 @@ +#include +#include +#include + +struct ListNode { + int val; + struct ListNode *next; +}; + +static struct ListNode *detectCycle(struct ListNode *head) +{ + if (head == NULL || head->next == NULL) { + return false; + } + + bool first = true; + struct ListNode *p0, *p1; + for (p0 = head, p1 = head; p1 != NULL && p1->next != NULL; p0 = p0->next, p1 = p1->next->next) { + if (p0 == p1 && !first) { + p0 = head; + while (p0 != p1) { + p0 = p0->next; + p1 = p1->next; + } + return p0; + } + first = false; + } + + return NULL; +} + +int main(int argc, char **argv) +{ + int i, count = argc - 1; + struct ListNode *head = NULL, *p, *prev; + for (i = 0; i < count; i++) { + p = malloc(sizeof(*p)); + p->val = atoi(argv[i + 1]); + p->next = NULL; + if (head == NULL) { + head = p; + } else { + prev->next = p; + } + prev = p; + } + p->next = head; + + p = detectCycle(head); + if (p != NULL) { + printf("%d\n", p->val); + } + return 0; +}