Therefore I went back to my original approach, and found a solution myself.

Here is the modified insert() function

C++

List *insert(List *head, int node, char *str) { int i; List *head2, *toBeInserted; head2 = head; for(i = 0; i < node; i++) head2 = head2->next; for(i = strlen(str); i >= 0 ; i--) { toBeInserted = (List *)malloc(sizeof(List)); toBeInserted->data = str[i]; if(node == 0) { toBeInserted->next = head; head = toBeInserted; } else { toBeInserted->next = head2->next; head2->next = toBeInserted; } } return head; }