forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path234.c
More file actions
38 lines (36 loc) · 847 Bytes
/
Copy path234.c
File metadata and controls
38 lines (36 loc) · 847 Bytes
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
/**
* 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;
}