234. Palindrome Linked List

来源:互联网 发布:马士兵大数据视频 编辑:程序博客网 时间:2024/05/22 00:24
/** * 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;    }};
0 0