```
class Solution {
public:
string longestPalindrome(string s) {
int head,tail,maxLength = 0;
string * result = nullptr;
for(int i = 0; i < s.length(); ++i)
{
//子串有奇数个元素
head = i - 1;
tail = i + 1;
while(head >= 0 && tail <= s.length() - 1)
{
if(s[head] == s[tail])
{
--head;
++tail;
}
else
{
break;
}
}
if(tail - head - 1 > maxLength)
{
maxLength = tail - head - 1;
if(result != nullptr)
{
delete result;
}
result = new string(&s[head+1],&s[tail]);
}
//子串有偶数个元素
head = i;
tail = i + 1;
while(head >= 0 && tail <= s.length() - 1)
{
if(s[head] == s[tail])
{
--head;
++tail;
}
else
{
break;
}
}
if(tail - head - 1 > maxLength)
{
maxLength = tail - head - 1;
if(result != nullptr)
{
delete result;
}
result = new string(&s[head+1],&s[tail]);
}
}
return *result;
}
};
```