NVIDIA Interview Question
Software Engineer / Developersvoid delnode(struct node* deleteme)
{
struct node *temp = deleteme->next;
deleteme->data = temp->data;
deleteme->next = temp->next;
free(temp);
}
Logic : Copy contents of next node to node to be deleted and delete the next node
It might run into NULL pointer problem. Also as mentioned in CC book, if the pointer is pointing to the last node, then it is not possible to remove that node.
void delNode(NODE *p){
NODE *q = NULL;
if(p == NULL) return;
if(p->next == NULL) {printf("cannot remove last node\n"; return}
q = p->next;
p->val = q->val;
p->next = q->next;
free(q);
return;
}
get the address of the node and the data associated. point it to the next node and get its data. copy the data of the next node to the current node and remove the next node.
- Anonymous March 15, 2011