Java-Easy-Reverse 2nd half of list and compare to first half

  • 1

    Slow and Fast pointers start with pointing to head. Move slow by 1 node, Fast by 2 nodes. When Fast reaches the end of the list, slow is at the middle node. Now call another function with slow as the head to reverse the latter half of the list. Then compare the 1st and the 2nd half.

    public boolean isPalindrome(ListNode head) {
            if(head==null|| return true;
            ListNode fast, slow;
            fast = slow = head;
            //fast will be null if number of nodes is even
            slow = reverseList(slow);
            fast = head;
            while(fast!=slow && slow!=null){
                    return false;
            return true;
        public ListNode reverseList(ListNode head) {
            if( return head;
            ListNode prev, curr, next;
            prev = next = null;
            curr = head;
                next =;
       = prev;
                prev = curr;
                curr = next;
            return prev;

Log in to reply

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