You are viewing a single comment's thread. Return to all comments →
We should also check if head == null then there we could write a different code like head= new Node();, head.data = data and finally return head and in the else part your code should be written?
There is no need to. If head is null then the new head of the list will point to null (that means that it is the only node in the list).