判断链表是否是回文。
我直接将链表的一半进行倒置,然后将两半的链表进行比较
/**
* 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) {
int ln = , i = ;
ListNode* rhead = NULL;
ListNode* now, *rnow;
for(now = head; now; now = now->next, ++ln);
for(now = head; i < ln / ; ++i){
ListNode* tnow = now;
now = now->next;
tnow->next = rhead;
rhead = tnow;
}
if(ln % == ){
now = now->next;
}
for(rnow = rhead; now && rnow; rnow = rnow->next, now = now->next){
if(rnow->val != now->val) return false;
}
return true;
}
};