Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: Phone Interview
node* DeleteNodes(node *head, int number)
{
if(head == null) return head;
node *outputhead = head;
while(head->data == number && head != null)
{
outputhead = head->next;
delete(head);
head = outputhead;
}
if(head == null) return outputhead;
while(head->next != null)
{
if(head->next->data == number)
{
node *tempnode = head->next;
head->next = tempnode->next;
delete(tempnode);
}
else
{
head = head->next;
}
}
return outputhead;
}
node* DeleteNodes(node *head, int number)
{
if(head == null) return head;
node *outputhead = head;
while(head->data == number && head != null)
{
outputhead = head->next;
delete(head);
head = outputhead;
}
if(head == null) return outputhead;
while(head->next != null)
{
if(head->next->data == number)
{
node *tempnode = head->next;
head->next = tempnode->next;
delete(tempnode);
}
else
{
head = head->next;
}
}
return outputhead;
}
void delete(struct node **head, int value)
{
struct node *temp,*current = *head;
if((*head)->data == value)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
while(current->next != NULL)
{
if (current->next->data == value)
{
temp = current->next;
current->next= current->next->next;
free(temp);
}
else
{
current = current->next;
}
}
}
void delete(node *head,int n)
{
node *current=head->next;
node *prev=head;
node *temp;
while(prev)
{
if(head->value==n)
{
temp=head;
head=head->next;
prev=current;
current=current->next;
}
elseif(current->value==n)
{
temp=current;
prev->next=current->next
current=current->next;
}
else
{prev=current; current=current->next;}
free(temp)
}
void del(node *&p,int num) //pass the root as p in main fn. and num as the
{ //node key value to be deleted...
if(!p)
return;
else
del(p->link,num);
if(p->key==num)
{
node *q;
q=p;
p=p->link;
delete q;
}
}
This recursive solution does not adjust the next pointer of previous node to the node after node which is to be deleted. You only change the the p. Deleting q will not update pointer of previous node to the correct next node.
public Node Delete(Node list, int data)
{
if (list == null)
return null;
Node llist = list,pre=null;
while (llist != null)
{
if (llist.Data == data)
{
if (pre == null)
{
list = list.Next;
llist = list;
continue;
}
else
{
pre.Next = llist.Next;
llist = pre;
}
}
pre = llist;
llist = llist.Next;
}
return list;
}
/* If the all the nodes contain same value and also need to delete that node contains that value. For example 10 nodes contains same value like 1, we have to delete the all the node,Finally it has to show the list is Empty */
void node_delete(struct node **head, int value)
{
struct node *temp,*current;
while(1)
{
if((*head)->data == value)
{
temp = *head;
*head = (*head)->next;
free(temp);
if( (*head) == NULL)
return ;
}
else
break;
}
current = *head;
while(current->next != NULL)
{
if (current->next->data == value)
{
temp = current->next;
current->next= current->next->next;
free(temp);
}
else
{
current = current->next;
}
}
}
/* Rewritten Format */
void node_delete(struct node **head, int value)
{
struct node *temp,*current;
while((*head)->data == value)
{
temp = *head;
*head = (*head)->next;
free(temp);
if((*head) == NULL)
return;
}
current = *head;
while(current->next != NULL)
{
if (current->next->data == value)
{
temp = current->next;
current->next= current->next->next;
free(temp);
}
else
{
current = current->next;
}
}
}
void delete(struct node **head, int value)
{
struct node *temp,*current = *head;
if((*head)->data == value)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
while(current->next != NULL)
{
if (current->next->data == value)
{
temp = current->next;
current->next= current->next->next;
free(temp);
}
else
{
current = current->next;
}
}
}
- loveCoding January 25, 2012