-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13-is_palindrome.c
66 lines (60 loc) · 1.03 KB
/
13-is_palindrome.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "lists.h"
/**
* reverse_listint - reverse linked list
* @head: first node of linked list
* Return: head
*/
void reverse_listint(listint_t **head)
{
listint_t *prev = NULL;
listint_t *current = *head;
listint_t *next = NULL;
while (current)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
/**
* is_palindrome - check if linked list is palindrome
* @head: pointer to pointer to first node of list
* Return: 1 palindrome, 0 otherwise
*/
int is_palindrome(listint_t **head)
{
listint_t *slow = *head, *fast = *head, *temp = *head, *dup = NULL;
if (*head == NULL || (*head)->next == NULL)
return (1);
while (1)
{
fast = fast->next->next;
if (!fast)
{
dup = slow->next;
break;
}
if (!fast->next)
{
dup = slow->next->next;
break;
}
slow = slow->next;
}
reverse_listint(&dup);
while (dup && temp)
{
if (temp->n == dup->n)
{
dup = dup->next;
temp = temp->next;
}
else
return (0);
}
if (!dup)
return (1);
return (0);
}