-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-is_palindrome.c
More file actions
executable file
·51 lines (44 loc) · 1.03 KB
/
Copy path13-is_palindrome.c
File metadata and controls
executable file
·51 lines (44 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "lists.h"
/**
* is_palindrome - Check if a singly linked list is a palindrome.
* @head: Adress to the list to check
*
* Return: 0 if it is not a palindrome, 1 if it is a palindrome
*/
int is_palindrome(listint_t **head)
{
listint_t *curr = *head;
listint_t *last_node = *head, *prev = NULL;
if (!head || !(*head))
return (1);
if (!(*head)->next)
return (1);
/* going to the end of the list */
while (last_node->next)
last_node = last_node->next;
while (curr)
{
/* if previous element is not null */
if (prev)
{
/* going to the element before the previous checked element */
while ((last_node->next != prev) && last_node->next)
last_node = last_node->next;
/* check if the last element did not change */
if (last_node == curr)
break;
}
if (last_node->n == curr->n)
{
/* check if the next element is not the last checked element */
if (curr->next == last_node)
break;
prev = last_node;
curr = curr->next;
last_node = curr;
}
else
return (0);
}
return (1);
}