Leetcode 234 Solution

This article provides solution to leetcode question 234 (palindrome-linked-list)

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;
    }
};