234. Palindrome Linked List

Problem:

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

Follow up:
Could you do it in O(n) time and O(1) space?

Solutions:

/**
 * 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) {
        ListNode* mid = middlePoint(head);
        ListNode* half = reverse(mid);

        //cout << mid->val << endl;
        //cout << half->val << endl;

        while (half) {
            //cout << head->val << " " << half->val << endl;
            if (head->val != half->val)  return false;
            head = head->next;
            half = half->next;
        }
        return true;
    }

    ListNode* middlePoint(ListNode* head) {
        ListNode* fast = head;
        ListNode* slow = head;
        while (fast && fast -> next) {
            fast = fast->next->next;
            slow = slow->next;
        }
        return slow;
    }

    ListNode* reverse(ListNode* head) {
        if (head == NULL)   return NULL;
        ListNode* dummy = new ListNode(0);
        ListNode* cur = head;
        ListNode* next = NULL;
        while (cur) {
            next = cur->next;
            cur->next = dummy->next;
            dummy->next = cur;
            cur = next;
        }
        return dummy->next;
    }
};

results matching ""

    No results matching ""