您的位置:首页 > 其它

234. Palindrome Linked List

2016-03-09 20:19 190 查看
/**
* 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||!head->next) return true;
//get len
ListNode* a=head;
int len=0;
while(a)
{
len++;
a=a->next;
}
//push
stack<ListNode*> s1;
a=head;
for(int i=0;i<len/2;i++)
{
s1.push(a);
a=a->next;
}
if(len%2) a=a->next;
for(int i=0;i<len/2;i++)
{
if(a->val==s1.top()->val) s1.pop();
a=a->next;
}
if(s1.size()) return false;
else return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: