Leetcode 234 Solution
This article provides solution to leetcode question 234 (palindrome-linked-list)
Access this page by simply typing in "lcs 234" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/palindrome-linked-list
Solution
/**
* 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) {
stack<int> s;
ListNode* p = head;
while (p)
{
s.push(p->val);
p = p->next;
}
p = head;
while (p)
{
if (p->val != s.top())
return false;
s.pop();
p = p->next;
}
return true;
}
};