Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 787 Bytes

palindrome.md

File metadata and controls

38 lines (29 loc) · 787 Bytes

234. Palindrome Linked List

class Solution {
public:
    bool isPalindrome(ListNode* head) {
        vector<int> ans;
        for (auto temp = head; temp != nullptr; temp = temp->next)
            ans.push_back(temp -> val);

        ListNode *a = nullptr;
        ListNode *b = head;
        ListNode *c = b -> next;

        while (c != nullptr) {
            b -> next = a;

            a = b;
            b = c;
            c = c -> next;
        }

        b -> next = a;
        int i = 0;
        for (auto temp = b; temp != nullptr; temp = temp->next) {
            if (temp -> val != ans[i++]) {
                return false;
            }
        }
        return true;

    }
};

O(1) space to be updated