Palindrome Linked List(cpp)
Palindrome Linked List(cpp)
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2 Output: false
Example 2:
Input: 1->2->2->1 Output: true
=========================================================================================
class Solution {
public:
bool isPalindrome(ListNode* head)
{
stack<int>st;
ListNode *temp;
temp=head;
while(temp)
{
st.push(temp->val);
temp=temp->next;
}
temp=head;
while(temp)
{
int x=st.top();
st.pop();
if(x!=temp->val)
return false;
temp=temp->next;
}
return true;
}
};
Comments
Post a Comment