void insertInPosition(node *&head, node *&tail, int d, int position) { if (position == 1) { insertAtHead(head, d); return; } node *temp = head; int count = 1; while (count < position - 1) { temp = temp->next; count++; } if (temp->next == NULL) { insertAtTail(tail, d); return; } node *nodeToInsert = new node(d); nodeToInsert->next = temp->next; temp->next = nodeToInsert; }