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;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.