Insert a Node at the Tail of a Linked List

  • + 3 comments

    you have not covered the condition where the head is null. if you want to do this in this manner , your code should be somthing like this:

    Node* Insert(Node *head,int data) { if(head==NULL) { Node *newn=(Node *)malloc(sizeof(Node));

            newn->next=NULL;
            newn->data=data;
            head=newn;
    
        }
    else{
         Node *newn2=(Node *)malloc(sizeof(Node));
        newn2=head;
        while(newn2->next!=NULL){
            newn2=newn2->next;
        }
        newn2->next=Insert(newn2->next , data);
    
    }
    return head;
    

    }