Linked list basics


So i created insertinmiddle() function for my linked list and in that if that position at which i want to insert the element is 0 , then i defined it separately using temp inside this function

But when i am printing the first element newly inserted does not show up however if i replace temp with head , the new element shows up

why this happens ?
because isn’t temp behaving same like head as i have taken pass by reference like for other positions ?

Hi @nischay1111

Change line 24 to node* &temp=head; Then it will make temp as a reference variable to head.

Hope it Helps.

But my question is that then why without changing anything it is working for all other locations
for other locations i am not doing “&temp=head” but still i am getting the solution right

because in all other locations , head needs to remain the same. But when inserting at start of the linked list, head is changed.