1ms JAVA beats all


  • 0
    D
    public class Solution {
        public boolean isPalindrome(ListNode head) {
            if (head ==null || head.next==null) return true;
            
            ListNode rev=null, slow=head, fast=head;
            
            while(fast!=null && fast.next!=null) {
                fast= fast.next.next;
                if (rev==null) {
                    rev = slow;
                    slow = slow.next;
                } else {
                    ListNode temp = slow;
                    slow = slow.next;
                    temp.next=rev;
                    rev= temp;
                }
            }
            
            if (fast!=null) slow=slow.next;
            
            while(slow!=null) {
                if (slow.val!=rev.val) return false;
                slow=slow.next;
                rev=rev.next;
            }
            
            return true;
        }
    }
    

Log in to reply
 

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