[Solved] How to choose proper if statements in linked list?


Consider the following Linked List:

HEAD ---> ["value2" | -]--> ["value3" | -]--> ["last_value" | null]

Assuming current points to “value3”:

HEAD ---> ["value2" | -]--> ["value3" | -]--> ["last_value" | null]
                                 ↑

then current.next != null is true, since current.next is actually the “next” node.

Now let’s assume current moved to “last_value”, now we’ll have:

HEAD ---> ["value2" | -]--> ["value3" | -]--> ["last_value" | null]
                                                    ↑

Now current != null is true whereas current.next is null (which is an indication that there’s no more nodes).

So “Choosing proper” if statement is not a good question since it really depends on what you want to check, they’re two different validations.

solved How to choose proper if statements in linked list?