Insert a node at a specific position in a linked list

  • + 0 comments

    In c language

    SinglyLinkedListNode* insertNodeAtPosition(SinglyLinkedListNode* llist, int data, int position) { struct Node *ptr = (struct Node *)malloc(sizeof(struct Node )); struct Node * p = llist; int i = 0;

    while (i!=position-1) {
        p = p->next;
        i++;
    }
    ptr->data=data;
    ptr->next = p->next;
    p->next = ptr;
    return llist;
    

    }