-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
234.c
42 lines (41 loc) · 846 Bytes
/
234.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
/**
* 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;
}