mirror of
https://github.com/TheAlgorithms/C
synced 2025-04-21 12:53:13 +03:00
43 lines
846 B
C
43 lines
846 B
C
/**
|
|
* Definition for singly-linked list.
|
|
* struct ListNode {
|
|
* int val;
|
|
* struct ListNode *next;
|
|
* };
|
|
*/
|
|
|
|
struct ListNode *reverse(struct ListNode *head)
|
|
{
|
|
struct ListNode *res = NULL;
|
|
while (head)
|
|
{
|
|
struct ListNode *pre_node = head;
|
|
head = head->next;
|
|
pre_node->next = res;
|
|
res = pre_node;
|
|
}
|
|
return res;
|
|
}
|
|
bool isPalindrome(struct ListNode *head)
|
|
{
|
|
struct ListNode *slow = head;
|
|
struct ListNode *fast = head;
|
|
struct ListNode *last;
|
|
while (fast && fast->next)
|
|
{
|
|
slow = slow->next;
|
|
fast = fast->next->next;
|
|
}
|
|
if (fast != NULL)
|
|
slow = slow->next;
|
|
last = reverse(slow);
|
|
while (last)
|
|
{
|
|
if (head->val != last->val)
|
|
return 0;
|
|
head = head->next;
|
|
last = last->next;
|
|
}
|
|
return 1;
|
|
}
|