public ListNode removeNodeWithCertainValue(ListNode firstNode, int value)
{
if (firstNode == null)
{
return null;
}
if (firstNode.NextListNode == null)
{
if(firstNode.NodeValue == value){
return null;
}
return firstNode;
}
//there may be multiple nodes with the given value
while (firstNode!= null && firstNode.NodeValue == value)
{
firstNode = firstNode.NextListNode;
}
//this check is needed in case all nodes have the provided value
if (firstNode != null)
{
firstNode.NextListNode = removeNodeWithCertainValue(firstNode.NextListNode, value);
}
return firstNode;
}
]]>{
if((next->a)==n)
{
printf(“Inside cond”);
curr->link=next->link;
break;
} else {
curr=curr->link;
next=next->link;
printf(“curr->a %d n”, curr->a);
printf(“next->a %d n”, next->a);
}
}
]]>struct node
{
int a;
struct node *link;
};
void add(struct node **p,int n)
{
struct node *temp,*r;
temp=*p;
if(*p==NULL)
{
temp=malloc(sizeof(struct node));
temp->a=n;
temp->link=NULL;
*p=temp;
}
else
{ temp=*p;
while(temp->link!=NULL)
{
temp=temp->link;
}
r=malloc(sizeof(struct node));
r->a=n;
temp->link=r;
r->link=NULL;
}
}
void display(struct node *a)
{
while(a!=NULL)
{
printf(“%d”,a->a);
a=a->link;
}
}
void delete(struct node **a,int n)
{
struct node *temp,*temp1;
temp=*a;
temp1=temp->link;
if(temp->a==n)
{
temp->link=NULL;
*a=temp1;
}
else
{
while(temp->link!=NULL)
{
if((temp->link->a)==n)
{
temp->link=temp->link->link;
}
temp=temp->link;
temp1=temp1->link;
}
}
}
void main()
{
struct node *p;
p=NULL;
add(&p,5);
add(&p,6);
add(&p,7);
add(&p,8);
display(p);
delete(&p,7);
printf(“n”);
display(p);
}
when i am trying this all working fine but last node is not getting deleted… any help
]]> private Node Delete(Node current, int key)
{
if (current == null) return null;
if (current.key == key)
{
current = current.next;
current = Delete(current, key);
}
else current.next = Delete(current.next, key);
return current;
}
1 void RemoveNodesWithValue(Node** head, int k) {
2 Node* toDelete;
3 while((*head) != NULL) {
4 if ((*head)->data == k) {
5 toDelete = (*head);
6 *head = (*head)-> next;
7 delete toDelete;
8 continue;
9 }
10 head = &((*head)->next);
11 }
]]>