-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13-is_palindrome.c
85 lines (68 loc) · 1.64 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "lists.h"
listint_t *reverse_listint(listint_t **head);
/**
* is_palindrome - Checks if a singly linked list is a palindrome.
*
* @head: head node.
*
* Return: 0 if it is not a palindrome, 1 if it is a palindrome.
*/
int is_palindrome(listint_t **head)
{
int palindrome, head_data, tail_data, list_len, tempCounter;
listint_t *head_node, *tail_node, *temp_node;
head_node = tail_node = temp_node = NULL;
list_len = 0;
palindrome = 1;
if (head == NULL)
return (palindrome = 0);
if (*head == NULL || (*head)->next == NULL)
return (palindrome);
temp_node = *head;
for (; temp_node; temp_node = temp_node->next)
list_len++;
head_node = tail_node = *head;
for (tempCounter = 1; tempCounter < (int)(list_len / 2); tempCounter++)
tail_node = tail_node->next;
if (list_len % 2 != 0)
tail_node = tail_node->next;
reverse_listint(&tail_node);
for (tempCounter = 0; tempCounter < (int)(list_len / 2); tempCounter++)
{
head_data = head_node->n;
tail_data = tail_node->n;
if (head_data != tail_data)
{
palindrome = 0;
break;
}
head_node = head_node->next;
tail_node = tail_node->next;
}
if (list_len % 2 != 0)
head_node = head_node->next;
reverse_listint(&head_node);
return (palindrome);
}
/**
* reverse_listint - reverse a linked list.
*
* @head: head node.
*
* Return: pointer to the first node.
*/
listint_t *reverse_listint(listint_t **head)
{
listint_t *temp = NULL, *ptr = NULL;
if (head == NULL || *head == NULL)
return (NULL);
temp = *head;
while (temp->next != NULL)
{
ptr = temp->next;
temp->next = (temp->next)->next;
ptr->next = *head;
*head = ptr;
}
return (*head);
}