Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ function to delete nodes in a linked list. The function takes two ar

ID: 3906396 • Letter: W

Question

Write a C++ function to delete nodes in a linked list. The function takes two arguments - the head of the linked list and the value to be deleted. It should delete all nodes which contain the value to be deleted. It should return the head of the linked list.


node * DeleteNode(node *head, int value);


The linked list structure:
struct node
{
int value;
node *next;
}; Write a C++ function to delete nodes in a linked list. The function takes two arguments - the head of the linked list and the value to be deleted. It should delete all nodes which contain the value to be deleted. It should return the head of the linked list.


node * DeleteNode(node *head, int value);


The linked list structure:
struct node
{
int value;
node *next;
}; Write a C++ function to delete nodes in a linked list. The function takes two arguments - the head of the linked list and the value to be deleted. It should delete all nodes which contain the value to be deleted. It should return the head of the linked list.


node * DeleteNode(node *head, int value);


The linked list structure:
struct node
{
int value;
node *next;
};

Explanation / Answer

Ans.Solution for this question is below:-

node * deleteNode(node* head, int value){

node* newHead=head;

node* prev;

while(newHead!=null){

if(newHead.value==value){

if(head==newHead){

head=head->next;

free(newHead);

break;

}else{

prev->next=newHead->next;

free(newHead);

break;

}

}

prev=newHead;

newHead=newHead->next;

}

return head;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote