Explanation: All occurrences of the given key = 1, is deleted from the Linked List.
Approach:
The Idea is to traverse the list while maintaining a prev pointer to track the previous node. If the current node contains the key, update the next pointer of the prev node to the current node, effectively removing it from the list. This process continues until the end of the list, ensuring all nodes with the specified key are removed.
Below is the implementation of the above idea:
C++
// Iterative C++ program to delete all occurrences of a // given key in a linked list#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intnew_data){data=new_data;next=nullptr;}};// Given the head of a list, delete all occurrences of a // given key and return the new head of the listNode*deleteOccurrences(Node*head,intkey){// Initialize pointers to traverse the linked listNode*curr=head,*prev=nullptr;// Traverse the list to delete all occurrenceswhile(curr!=nullptr){// If current node's data is equal to keyif(curr->data==key){// If node to be deleted is head nodeif(prev==nullptr){head=curr->next;}else{prev->next=curr->next;}// Move to the next nodecurr=curr->next;}else{// Move pointers one position aheadprev=curr;curr=curr->next;}}returnhead;}voidprintList(Node*curr){while(curr!=nullptr){cout<<" "<<curr->data;curr=curr->next;}}intmain(){// Create a hard-coded linked list:// 2 -> 2 -> 1 -> 8 -> 2 -> NULLNode*head=newNode(2);head->next=newNode(2);head->next->next=newNode(1);head->next->next->next=newNode(8);head->next->next->next->next=newNode(2);intkey=2;head=deleteOccurrences(head,key);printList(head);return0;}
C
// Iterative C program to delete all occurrences of a // given key in a linked list#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*next;};// Given the head of a list, delete all occurrences of a // given key and return the new head of the liststructNode*deleteOccurrences(structNode*head,intkey){// Initialize pointers to traverse the linked liststructNode*curr=head,*prev=NULL;// Traverse the list to delete all occurrenceswhile(curr!=NULL){// If current node's data is equal to keyif(curr->data==key){// If node to be deleted is head nodeif(prev==NULL){head=curr->next;}else{prev->next=curr->next;}// Move to the next nodecurr=curr->next;}else{// Move pointers one position aheadprev=curr;curr=curr->next;}}returnhead;}voidprintList(structNode*curr){while(curr!=NULL){printf(" %d",curr->data);curr=curr->next;}}structNode*createNode(intnew_data){structNode*new_node=(structNode*)malloc(sizeof(structNode));new_node->data=new_data;new_node->next=NULL;returnnew_node;}intmain(){// Create a hard-coded linked list:// 2 -> 2 -> 1 -> 8 -> 2 -> NULLstructNode*head=createNode(2);head->next=createNode(2);head->next->next=createNode(1);head->next->next->next=createNode(8);head->next->next->next->next=createNode(2);intkey=2;head=deleteOccurrences(head,key);printList(head);return0;}
Java
// Java program to delete all occurrences of a given key // in a linked listclassNode{intdata;Nodenext;Node(intnew_data){data=new_data;next=null;}}// Given the head of a list, delete all occurrences of a // given key and return the new head of the listpublicclassGfG{staticNodedeleteOccurrences(Nodehead,intkey){// Initialize pointers to traverse the linked listNodecurr=head,prev=null;// Traverse the list to delete all occurrenceswhile(curr!=null){// If current node's data is equal to keyif(curr.data==key){// If node to be deleted is head nodeif(prev==null){head=curr.next;}else{prev.next=curr.next;}// Move to the next nodecurr=curr.next;}else{// Move pointers one position aheadprev=curr;curr=curr.next;}}returnhead;}// This function prints the contents of the linked list // starting from the headstaticvoidprintList(Nodecurr){while(curr!=null){System.out.print(" "+curr.data);curr=curr.next;}}publicstaticvoidmain(String[]args){// Create a hard-coded linked list:// 2 -> 2 -> 1 -> 8 -> 2 -> NULLNodehead=newNode(2);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(8);head.next.next.next.next=newNode(2);intkey=2;head=deleteOccurrences(head,key);printList(head);}}
Python
# Python program to delete all occurrences of a given key # in a singly linked listclassNode:def__init__(self,new_data):self.data=new_dataself.next=None# Given the head of a list, delete all occurrences of a # given key and return the new head of the listdefdelete_occurrences(head,key):# Initialize pointers to traverse the linked listcurr=headprev=None# Traverse the list to delete all occurrenceswhilecurrisnotNone:# If current node's data is equal to keyifcurr.data==key:# If node to be deleted is head nodeifprevisNone:head=curr.nextelse:prev.next=curr.next# Move to the next nodecurr=curr.nextelse:# Move pointers one position aheadprev=currcurr=curr.nextreturnheaddefprint_list(curr):whilecurrisnotNone:print(f" {curr.data}",end="")curr=curr.nextprint()if__name__=="__main__":# Create a hard-coded linked list:# 2 -> 2 -> 1 -> 8 -> 2 -> Nonehead=Node(2)head.next=Node(2)head.next.next=Node(1)head.next.next.next=Node(8)head.next.next.next.next=Node(2)key=2head=delete_occurrences(head,key)print_list(head)
C#
// C# program to delete all occurrences of a given key // in a singly linked listusingSystem;classNode{publicintData;publicNodenext;publicNode(intnewData){Data=newData;next=null;}}classGfG{// Function to delete all occurrences of a // given key and return the new head of the liststaticNodeDeleteOccurrences(Nodehead,intkey){// Initialize pointers to traverse the linked listNodecurr=head;Nodeprev=null;// Traverse the list to delete all occurrenceswhile(curr!=null){// If current node's data is equal to keyif(curr.Data==key){// If node to be deleted is head nodeif(prev==null){head=curr.next;}else{prev.next=curr.next;}// Move to the next nodecurr=curr.next;}else{// Move pointers one position aheadprev=curr;curr=curr.next;}}returnhead;}staticvoidPrintList(Nodecurr){while(curr!=null){Console.Write(" "+curr.Data);curr=curr.next;}Console.WriteLine();}staticvoidMain(){// Create a hard-coded linked list:// 2 -> 2 -> 1 -> 8 -> 2 -> NULLNodehead=newNode(2);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(8);head.next.next.next.next=newNode(2);intkey=2;head=DeleteOccurrences(head,key);PrintList(head);}}
JavaScript
// JavaScript program to delete all occurrences of a given key // in a singly linked listclassNode{constructor(newData){this.data=newData;this.next=null;}}// Given the head of a list, delete all occurrences of a given // key and return the new head of the listfunctiondeleteOccurrences(head,key){// Initialize pointers to traverse the linked listletcurr=head;letprev=null;// Traverse the list to delete all occurrenceswhile(curr!==null){// If current node's data is equal to keyif(curr.data===key){// If node to be deleted is head nodeif(prev===null){head=curr.next;}else{prev.next=curr.next;}// Move to the next nodecurr=curr.next;}else{// Move pointers one position aheadprev=curr;curr=curr.next;}}returnhead;}functionprintList(curr){while(curr!==null){console.log(" "+curr.data);curr=curr.next;}console.log();}// Create a hard-coded linked list:// 2 -> 2 -> 1 -> 8 -> 2 -> NULLlethead=newNode(2);head.next=newNode(2);head.next.next=newNode(1);head.next.next.next=newNode(8);head.next.next.next.next=newNode(2);letkey=2;head=deleteOccurrences(head,key);printList(head);
Output
1 8
Time complexity: O(n), where n is the number of nodes in the list. Auxiliary Space: O(1)