Why we set next node null in node constructor

Why we set next node null in node constructor ??

    static class Node{
        int data;
        Node next;
        Node(int d){
            data = d;
            next = null;
        }
    }

Its Java

as per my knowledge, setting the default value for next to NULL help in reducing memory wastage, and also helps to keep track on the end of the chain.

you can use other methodes too, like using a index counter, and terminating the chain when you get to the desired index.
but i would say setting the next as NULL is way better option

Thanks @shantanu_1404 For The Answer

Please More Answers
!!

When we create a fresh object of node class we don’t want it to point to another node by default so we set it to null
For example: When we want to insert a node to an empty linked list, it will not have the next node

Thanks @saoodahmad

But This Is Also Working

    static class Node{
        int data;
        Node next;
        Node(int d){
            data = d;
        }
    }
1 Like

Thanks @ssjgz

1 Like