Hello, I was going through the Programming Interviews Exposed book. There's a code given for inserting an element at the front of linked lists.
bool insertInFront( IntElement **head, int data ){
IntElement *newElem = new IntElement;
if( !newElem ) return false;
newElem->data = data;
*head = newElem;
return true;
}
IMHO this code forgets to update the next pointer of the new element, doesnt it ? Although I am sure the code is wrong, I just want to confirm my linked list concepts are not horribly wrong.
I believe the code should add the following line at the right place.
newElem->next = *head;
Can someone please just tell me whether I am right or wrong ?
Thanks!