We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I was doing same mistake earlier by not handling the use case when head pointer of linked list is NULL. This is one of the sample test case. So, when head pointer is null then we have to set head pointer to the new linked list node.
if (head == null){
head = new Node();
head.data = data;
}
Insert a Node at the Tail of a Linked List
You are viewing a single comment's thread. Return to all comments →
I was doing same mistake earlier by not handling the use case when head pointer of linked list is NULL. This is one of the sample test case. So, when head pointer is null then we have to set head pointer to the new linked list node.
Adding above if statement solved my problem. Im general, we just have to traverse the linked list and add a new node after tail node of linked list.