why can't i pass in the case of [1,1,2,1]


  • 0
    S
    public boolean isPalindrome(ListNode head) {
        if(head == null){
            return true;
        }
        ListNode reverseHead = reverse(head);
        while( head != null && reverseHead != null) {
            if(head.val != reverseHead.val){
                return false;
            }else {
                head = head.next;
                reverseHead = reverseHead.next;
            }
        }
        return true;
    }
    
    public ListNode reverse(ListNode head){
        ListNode header = new ListNode(0);
        header.next = head;
        ListNode cur = header.next;
        while(cur != null && cur.next != null){
            ListNode n = cur.next;
            cur.next = n.next;
            n.next = header.next;
            header.next = n;
        }
        return header.next;
    }
    

    }


Log in to reply
 

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