Leetcode 234 Palindrome Linked List 链表

判断链表是否是回文。

我直接将链表的一半进行倒置,然后将两半的链表进行比较

 /**
* 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;
}
};
上一篇:Cheatsheet: 2016 06.01 ~ 6.30


下一篇:jenkins+ant+jmeter自动化环境搭建(一)