JAVA-----------------Easy Version To UnderStand!!!!!!!!!!!!!!


  • -1
    H
        public boolean isPalindrome(ListNode head) {
        if(head==null)
         return true;
        if(head.next==null)
          return true;
        ListNode slow=head,fast=head;
        while(fast.next!=null&&fast.next.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        ListNode tmp=slow;
        slow=slow.next;
        tmp.next=null;
        ListNode head2=new ListNode(0);
        head2.next=null;
        while(slow!=null){
            tmp=slow.next;
            slow.next=head2.next;
            head2.next=slow;
            slow=tmp;
        }
        ListNode p=head,q=head2.next;
        while(q!=null){
            if(p.val!=q.val)
              return false;
              p=p.next;
              q=q.next;
        }
        return true;
    }

Log in to reply
 

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