Delete all Occurrence of the key from the given linked list
“Delete all Occurrence of the key from the given linked list” is again one of the important problem asked in many technical interview based on linked list. Here, we are given a linked list and our task is to delete all occurrences of a given key ‘x’ from a linked list.
For example, we need to delete all occurrences of ‘45’ from a given list. List: 45 -> 55 -> 32 -> 45 -> 66 -> 71 -> 45 -> 33 -> 45 After deleting all occurrences of ‘45’, the list will become: List: 55 -> 32 -> 66 -> 71 -> 33
The steps required to Delete all Occurrence of the key from the given linked list is as follows:
- Before traversing the complete linked list, we will delete all beginning occurrences of given key ‘x’ and adjust our head accordingly.
- Now, after adjusting the head of the linked list, we will traverse the complete linked list and delete all occurrences of given key ‘x’ from a linked list.
C++ Program to Delete all Occurrence of the key from the given linked list is as follows:
/* C++ Program to delete all occurrences of the given key from the Linked List */
#include<bits/stdc++.h>
using namespace std;
/* Structure of the node of the linked list */
typedef struct node
{
int data;
struct node *next;
}Node;
/* Function to Insert node at the end of the linked list */
void insert_end(Node **head, int ele)
{
/* Creating a new node */
Node *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = ele;
new_node->next = NULL;
/* check whether the linked list is empty or not */
/* If Linked List is empty, then new node will become head of the linked list */
if(*head == NULL)
{
*head = new_node;
return;
}
/* If Linked List is not empty, then traverse the complete linked list and point the pointer
of last node of the linked list to the new node */
Node *temp = NULL;
temp = (*head);
while(temp->next != NULL)
temp=temp->next;
temp->next = new_node;
}
/* Function to delete all occurrences of a given key from a linked list */
Node *deleteAllx(Node *head , int x)
{
/* If Linked List is empty, return head */
if(head == NULL)
return head;
/* Delete All occurrences of the given key from the beginning, and adjust head accordingly */
while(head!=NULL && head->data==x)
head=head->next;
/* Now, delete remaining occurrences of the key from the linked list by storing the previous
node and pointing it to desired location accordingly */
Node* temp=head;
while(temp->next != NULL)
{
if(temp->next->data == x)
{
Node *p = temp->next;
temp->next=temp->next->next;
delete p;
}
else
{
temp=temp->next;
}
}
return head;
}
/* Function to Print the Linked List */
void print(Node *head)
{
Node *temp = head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main()
{
Node *head;
head = NULL;
/* Inserting Some Nodes in the Linked List */
insert_end(&head,45);
insert_end(&head,78);
insert_end(&head,45);
insert_end(&head,93);
insert_end(&head,45);
insert_end(&head,99);
insert_end(&head,45);
cout<<"Linked List before deleting all occurrences of 45:\n";
print(head);
head = deleteAllx(head,45);
cout<<"\nLinked List after deleting all occurrences of 45:\n";
print(head);
}
OUTPUT: Linked List before deleting all occurrences of 45: 45 78 45 93 45 99 45 Linked List after deleting all occurrences of 45: 78 93 99
Related Posts:
- Delete Complete Linked List.
- Delete Nth Node of the Linked List.
- Delete without head pointer of the Linked List.
- Delete Alternate Nodes of the Linked List.
- Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
- Remove Duplicate Nodes from an Unsorted Linked List.
- Remove Duplicate Nodes from a Sorted Linked List.
- Find Union and Intersection of Two Linked List.
- Merge Two Sorted Linked List.
- Insert a New Node at the Sorted Linked List.
- Reverse a Linked List.
- Reverse a Linked List Using Stack.
- Printing Linked List in Reverse Order without actually reversing the Linked List.
- Swap Adjacent Elements of the Linked List.
- Count All Occurrences of a Particular Node in a Linked List.
- Bubble Sort on Linked List.
- Detect a Loop in a Linked List.
- Find the Length of the Loop present in the Linked List.
- Detect and Remove Loop from a Linked List.
- Segregate Even and Odd Nodes of the Linked List.