[Solved] Inserting linked list at the end [closed]


you should have to return inside if because after if statement it again comes into while loop and add extra node at the end

struct node
{
    int data; // node format
    struct node* next;
};

void insertAtEnd(struct node** headRef, int newData)
{
    struct node* ptr;
    struct node* newnode;
    ptr = (*headRef);
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = newData;
    newnode->next = NULL;
    if (*headRef == NULL)
    {
        *headRef = newnode;
         return;

    }
    while (ptr->next != NULL)
    {
        ptr = ptr->next;
    }
    ptr->next = newnode;
    return;
}

solved Inserting linked list at the end [closed]