/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head == NULL || head->next == NULL )
return true;
int num = 0;
ListNode * h = head;
while(h != NULL){
num++;
h = h->next;
}
ListNode * b=head,*c,*d;
for(int i = 0;i < num / 2; i++){
c = b;
b = b->next;
}
if(num % 2 == 1) b = b->next;
c->next = NULL;
for(d=head,c=head->next;c != NULL;){
h = c;
c = c->next;
h->next = d;
d = h;
}
head->next = NULL;
while(b != NULL){
if(b->val != d->val) return false;
d = d->next;
b = b->next;
}
return true;
}
};
leetcode 234. Palindrome Linked List
最新推荐文章于 2024-09-21 10:02:17 发布