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;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter