题解 | 回文链表
回文链表
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;
}
};

查看16道真题和解析