Skip to content

Latest commit

 

History

History
 
 

234. Palindrome Linked List

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

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?

Companies:
IXL, Microsoft, Google

Related Topics:
Linked List, Two Pointers

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/palindrome-linked-list/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
private:
    int getLength(ListNode *head) {
        int len = 0;
        for (; head; head = head->next, ++len);
        return len;
    }
    ListNode *reverse(ListNode *head) {
        ListNode dummy(0);
        while (head) {
            ListNode *node = head;
            head = head->next;
            node->next = dummy.next;
            dummy.next = node;
        }
        return dummy.next;
    }
public:
    bool isPalindrome(ListNode* head) {
        if (!head) return true;
        int len = (getLength(head) + 1) / 2;
        ListNode *p = head, *q;
        while (--len > 0) p = p->next;
        q = p->next;
        p->next = NULL;
        q = reverse(q);
        while (head && q) {
            if (head->val != q->val) return false;
            head = head->next;
            q = q->next;
        }
        return true;
    }
};