回文链表
请判断一个链表是否为回文链表。
示例 1:
输入: 1->2 输出: false
示例 2:
输入: 1->2->2->1 输出: true
进阶:
你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
思路:利用栈
class Solution {
public:
bool isPalindrome(ListNode* head) {
stack<int> s;
ListNode* temp=head;
while(head!=NULL){
s.push(head->val);
head=head->next;
}
while(!empty(s)){
if(temp->val!=s.top())return false;
s.pop();
temp=temp->next;
}
return true;
}
};