Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

 /**
* 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) { vector<int> tmp;
while(head)
{
tmp.push_back(head->val);
head=head->next;
}
int left=;
int right=tmp.size()-;
while(left<right)
{
if(tmp[left]!=tmp[right])
return false;
left++;
right--;
}
return true;
}
};
上一篇:每天一个linux命令(62):sh命令 /Linux中执行shell脚本的4种方法总结


下一篇:百度热搜爬取并制作词云图