Problem: Palindrome Linked List

Posted by Marcy on February 18, 2015

Question

Given a singly linked list, determine if it is a palindrome.

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

Solution

TODO

Code

class Solution {
    public boolean isPalindrome(ListNode head) {
        ListNode w = head;
        ListNode r = head;
        while(r != null && r.next != null) {
            r = r.next.next;
            w = w.next;
        }
        
        // find the middle of the list
        // skip the middle
        if(r != null) {
            w = w.next;
        }
        
        ListNode head2 = reverse(w);
        
        // head2 should have equal length or 1 node shorter
        while(head2 != null) {
            if(head.val != head2.val) return false;
            head = head.next;
            head2 = head2.next;
        }
        
        return true;
    }
    
    public ListNode reverse(ListNode head) {
        ListNode newHead = null;
        while(head != null) {
            ListNode tmp = head;
            head = head.next;
            tmp.next = newHead;
            newHead = tmp;
        }
        
        return newHead;
    }
}

Performance

TODO