Why this while statement cause nullPointerException

  • 0
    public class Solution {
    public ListNode oddEvenList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode odd = head;
        ListNode even = head.next;
        ListNode evenHead = even;
        while(even.next != null && even != null){
            odd.next = odd.next.next;
            odd = odd.next;
            even.next = even.next.next;
            even = even.next;
        odd.next = evenHead;
        return head;

    Why I use while(even.next != null && even != null) cause nullPointerException, but while(even != null && even.next != null) works well. If it is the problem of "&&", I tried while(even.next != null & even != null), but still cause the same problem. Could you tell me why?

    Thank you in advance

  • 0

    even.next != null && even != null, please exchange the position,even though it still can't pass

  • 0

    You must be new to Java (or C++, it uses exactly the same "if" evaluation technique in this situation).

    First, let's talk about operator "&" and operator "&&".
    First operator is bitwise AND Operator (https://msdn.microsoft.com/en-us/library/z0zec0b2.aspx):

    The bitwise AND operator (&) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
    Both operands to the bitwise AND operator must be of integral types. The usual arithmetic conversions covered in Arithmetic Conversions, are applied to the operands.

    This operator can be used in if constructions but it's not suitable here.

    Second operator is a Logical AND Operator (https://msdn.microsoft.com/en-us/library/c6s3h5a7.aspx):

    The logical AND operator (&&) returns the boolean value true if both operands are true and returns false otherwise. The operands are implicitly converted to type bool prior to evaluation, and the result is of type bool. Logical AND has left-to-right associativity.
    The operands to the logical AND operator need not be of the same type, but they must be of integral or pointer type. The operands are commonly relational or equality expressions.

    You intended to use && operator here so you should stick to it.

    Now let's talk about how this if constructions are evaluated and executed.

    operator && evaluates left operand first and if the value is logically false then it avoids evaluating the right operand. Typical use is for example if (x > 0 && k/x < limit) ... that avoids division by zero problems.

    In your case, you're evaluating two nodes on each iteration, even and even.next. Both of them could be null. But if even node is null - even.next does not even exist and referencing it would cause nullPointer Exception. You could think of it just like as division be zero. So in your if construction you should first check of node even is not null and then check if even.next is not null. If even node is null Jva won't check even.next because the statement is already false

Log in to reply

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