My java solution beats 91% submissions


  • -1
    S

    public class Solution {
    public boolean isPalindrome(ListNode head) {

        ListNode p1 = head;
        ListNode p2 = head;
        ListNode p3 = head;
        ListNode p4 = null;
        
        while(p2 != null && p2.next != null) {
            p2 = p2.next.next;
            p1 = p1.next;
            p3.next = p4;
            p4 = p3;
            p3 = p1;
        }
        if(p2 != null) {
            p1 = p1.next;
        }
        
        while(p1 != null && p4 != null) {
            if(p1.val != p4.val) return false;
            p1 = p1.next;
            p4 = p4.next;
        }
        
        return p1  == null && p4 == null;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.