Skip to content

Commit f788004

Browse files
committed
more commits
1 parent 93fb9b6 commit f788004

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Diff for: 0x13-more_singly_linked_lists/103-find_loop.c

+38
Original file line numberDiff line numberDiff line change
@@ -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+
tortoise = tortoise->next;
34+
hare = (hare->next)->next;
35+
}
36+
37+
return (NULL);
38+
}

0 commit comments

Comments
 (0)