We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 93fb9b6 commit f788004Copy full SHA for f788004
0x13-more_singly_linked_lists/103-find_loop.c
@@ -0,0 +1,38 @@
1
+#include "lists.h"
2
+
3
+/**
4
+ * find_listint_loop - Finds the loop contained in a list
5
+ * @head: A pointer to the head of the list
6
+ * Return: If there is no loop - NULL.
7
+ */
8
+listint_t *find_listint_loop(listint_t *head)
9
+{
10
+ listint_t *tortoise, *hare;
11
12
+ if (head == NULL || head->next == NULL)
13
+ return (NULL);
14
15
+ tortoise = head->next;
16
+ hare = (head->next)->next;
17
18
+ while (hare)
19
+ {
20
+ if (tortoise == hare)
21
22
+ tortoise = head;
23
24
+ while (tortoise != hare)
25
26
+ tortoise = tortoise->next;
27
+ hare = hare->next;
28
+ }
29
30
+ return (tortoise);
31
32
33
34
+ hare = (hare->next)->next;
35
36
37
38
+}
0 commit comments