简体   繁体   中英

Why does insertion work in linked list when I put node!= null

I am trying to insert a node to the end of a linked list. I noticed that when I put node.next.= null in the while loop it works but not when I put node?= null. Can you please tell me why is it like that?

This works

while(node.next!=null){
node = node.next;
    }
node.next = new Node(int 5);

This wont work.

while(node!=null){
        
  node = node.next;
    }
node = new Node(int 5);

Because when node.next is null it does not mean that it is pointing to some memory zone which has no object, but it means that is not pointing to anything.

So when you assign null to node you are not in some memory being pointed by someone, but you are literally nothing and nothing is pointing to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM