diff --git a/141-linked-list-cycle/141-linked-list-cycle.c b/141-linked-list-cycle/141-linked-list-cycle.c new file mode 100644 index 0000000..ecbde27 --- /dev/null +++ b/141-linked-list-cycle/141-linked-list-cycle.c @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +bool hasCycle(struct ListNode *head) { + struct ListNode *current = head; + int index = 0; + while (current) { + struct ListNode *follow = head; + int follow_index = 0; + while (follow != current) { + follow = follow->next; + ++follow_index; + } + + if (follow_index < index) { + return true; + } + + current = current->next; + ++index; + } + return false; +} \ No newline at end of file