• + 1 comment

    this code is similar to yours but it gives a null pointer exception.can you explain why?

    if(head==null){
        return;
    }
    else{
        head=head.next;
        ReversePrint(head);
        System.out.println(head.data);
    }
    
    • + 0 comments

      Because you are first putting it as head.next which means if it IS null, it will already be at head.next and the return will basically return to the pre-recursion of the last function just to get print a null value

      What his code does is, if head.next is null, it will return so that head.data will be printed, otherwise, head=head.next will happen and that will be put into the same function