题解 | 回文链表

回文链表

https://www.nowcoder.com/practice/baefd05def524a92bcfa6e1f113ed4f0

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
#include <string>
class Palindrome {
public:
    bool isPalindrome(ListNode* pHead) {
        // write code here
        if (!pHead || !pHead->next) {
            return true;
        }
        string sHead;
        auto ph = pHead;
        while(ph){
            sHead += to_string(ph->val);
            ph=ph->next;
        }
        size_t len = sHead.length();
        for(size_t i =0;i<len;++i){
            if(sHead[i] !=sHead[len-i-1])
            return false;
        }
        return true;
    }
};

全部评论

相关推荐

点赞 评论 收藏
分享
09-21 21:14
门头沟学院
否极泰来来来来:和他说:这里不好骂你,我们加个微信聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务