Insert a Node at the Tail of a Linked List

  • + 6 comments

    SinglyLinkedListNode* insertNodeAtTail(SinglyLinkedListNode* head, int data) {

        SinglyLinkedListNode *new_node,*ptr;
        new_node = new SinglyLinkedListNode();
        new_node->data = data;
        new_node->next = NULL;
        if(head == NULL){
            head = new_node;
            return head;
        }else{
            ptr = head;
            while(ptr != NULL){
                ptr = ptr->next;
            }
            ptr->next = new_node;
            new_node->next = NULL;
    return head;    
        }
    

    }

    can you please help me with this code?