NVIDIA Interview Question
Software Engineer / Developers/* Node data structure */
struct node_data
{
int a;
char b;
};
/*Linked list node structure */
struct node
{
struct node_data* data;
struct node* next;
};
void delete_last(struct node **head)
{
if( *head ==NULL)
return;
if(*head->next = NULL) //Only one node
{
free(*head->data);
free(*head);
*head =NULL;
return;
}
struct node* current = *head;
while(current->next->next )
current = current->next;
free(current->next->data);
free(current->next);
current->next = NULL;
return;
}
Rascala coder mind it... All testing done... Rajnikanth rocks!
void Delete(node **head)
{
node *p=*head;
if(p==NULL) return;
if(p->next==NULL) {*head=NULL; free(p->data); free(p); return; }
node *q=p;
p=p->next;
while(p->next!=NULL) { p=p->next; q=q->next; }
q->next=q->next->next; //thats null, do check here!
free(p->data);
free(p);
}
Rascala coder mind it... All testing done... Rajnikanth rocks!
void Delete(node **head)
{
node *p=*head;
if(p==NULL) return;
if(p->next==NULL) {*head=NULL; free(p->data); free(p); return; }
node *q=p;
p=p->next;
while(p->next!=NULL) { p=p->next; q=q->next; }
q->next=q->next->next; //thats null, do check here!
free(p->data);
free(p);
}
Rascala coder mind it... All testing done... Rajnikanth rocks!
void Delete(node **head)
{
node *p=*head;
if(p==NULL) return;
if(p->next==NULL) {*head=NULL; free(p->data); free(p); return; }
node *q=p;
p=p->next;
while(p->next!=NULL) { p=p->next; q=q->next; }
q->next=q->next->next; //thats null, do check here!
free(p->data);
free(p);
}
Rascala coder mind it... All testing done... Rajnikanth rocks!
void Delete(node **head)
{
node *p=*head;
if(p==NULL) return;
if(p->next==NULL) {*head=NULL; free(p->data); free(p); return; }
node *q=p;
p=p->next;
while(p->next!=NULL) { p=p->next; q=q->next; }
q->next=q->next->next; //thats null, do check here!
free(p->data);
free(p);
}
node*temp,*cur /*s points to dummy node at the beginning of the list*/
- Anonymous July 17, 2011cur=s;
temp=s->next;
while(temp!=NULL)
{
cur=temp;
temp=temp->next;
}
free(cur->dataptr);
free(cur);