Delete all occurrences of a given key in a linked list
Last Updated :
04 Sep, 2024
Given a singly linked list, the task is to delete all occurrences of a given key in it.
Examples:
Input: head: 2 -> 2 -> 1 -> 8 -> 2 -> NULL, key = 2
Output: 1 -> 8 -> NULL
Explanation: All occurrences of the given key = 2, is deleted from the Linked List
Input: head: 1 -> 1 -> 1 -> 4 -> 1 -> NULL, key = 1
Output: 4 -> NULL
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>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int new_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 list
Node* deleteOccurrences(Node* head, int key) {
// Initialize pointers to traverse the linked list
Node *curr = head, *prev = nullptr;
// Traverse the list to delete all occurrences
while (curr != nullptr) {
// If current node's data is equal to key
if (curr->data == key) {
// If node to be deleted is head node
if (prev == nullptr) {
head = curr->next;
}
else {
prev->next = curr->next;
}
// Move to the next node
curr = curr->next;
}
else {
// Move pointers one position ahead
prev = curr;
curr = curr->next;
}
}
return head;
}
void printList(Node* curr) {
while (curr != nullptr) {
cout << " " << curr->data;
curr = curr->next;
}
}
int main() {
// Create a hard-coded linked list:
// 2 -> 2 -> 1 -> 8 -> 2 -> NULL
Node* head = new Node(2);
head->next = new Node(2);
head->next->next = new Node(1);
head->next->next->next = new Node(8);
head->next->next->next->next = new Node(2);
int key = 2;
head = deleteOccurrences(head, key);
printList(head);
return 0;
}
C
// Iterative C program to delete all occurrences of a
// given key in a linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Given the head of a list, delete all occurrences of a
// given key and return the new head of the list
struct Node* deleteOccurrences(struct Node* head, int key) {
// Initialize pointers to traverse the linked list
struct Node *curr = head, *prev = NULL;
// Traverse the list to delete all occurrences
while (curr != NULL) {
// If current node's data is equal to key
if (curr->data == key) {
// If node to be deleted is head node
if (prev == NULL) {
head = curr->next;
}
else {
prev->next = curr->next;
}
// Move to the next node
curr = curr->next;
}
else {
// Move pointers one position ahead
prev = curr;
curr = curr->next;
}
}
return head;
}
void printList(struct Node* curr) {
while (curr != NULL) {
printf(" %d", curr->data);
curr = curr->next;
}
}
struct Node* createNode(int new_data) {
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
int main() {
// Create a hard-coded linked list:
// 2 -> 2 -> 1 -> 8 -> 2 -> NULL
struct Node* 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);
int key = 2;
head = deleteOccurrences(head, key);
printList(head);
return 0;
}
Java
// Java program to delete all occurrences of a given key
// in a linked list
class Node {
int data;
Node next;
Node(int new_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 list
public class GfG {
static Node deleteOccurrences(Node head, int key) {
// Initialize pointers to traverse the linked list
Node curr = head, prev = null;
// Traverse the list to delete all occurrences
while (curr != null) {
// If current node's data is equal to key
if (curr.data == key) {
// If node to be deleted is head node
if (prev == null) {
head = curr.next;
}
else {
prev.next = curr.next;
}
// Move to the next node
curr = curr.next;
}
else {
// Move pointers one position ahead
prev = curr;
curr = curr.next;
}
}
return head;
}
// This function prints the contents of the linked list
// starting from the head
static void printList(Node curr) {
while (curr != null) {
System.out.print(" " + curr.data);
curr = curr.next;
}
}
public static void main(String[] args) {
// Create a hard-coded linked list:
// 2 -> 2 -> 1 -> 8 -> 2 -> NULL
Node head = new Node(2);
head.next = new Node(2);
head.next.next = new Node(1);
head.next.next.next = new Node(8);
head.next.next.next.next = new Node(2);
int key = 2;
head = deleteOccurrences(head, key);
printList(head);
}
}
Python
# Python program to delete all occurrences of a given key
# in a singly linked list
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
# Given the head of a list, delete all occurrences of a
# given key and return the new head of the list
def delete_occurrences(head, key):
# Initialize pointers to traverse the linked list
curr = head
prev = None
# Traverse the list to delete all occurrences
while curr is not None:
# If current node's data is equal to key
if curr.data == key:
# If node to be deleted is head node
if prev is None:
head = curr.next
else:
prev.next = curr.next
# Move to the next node
curr = curr.next
else:
# Move pointers one position ahead
prev = curr
curr = curr.next
return head
def print_list(curr):
while curr is not None:
print(f" {curr.data}", end="")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hard-coded linked list:
# 2 -> 2 -> 1 -> 8 -> 2 -> None
head = 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 = 2
head = delete_occurrences(head, key)
print_list(head)
C#
// C# program to delete all occurrences of a given key
// in a singly linked list
using System;
class Node {
public int Data;
public Node next;
public Node(int newData) {
Data = newData;
next = null;
}
}
class GfG {
// Function to delete all occurrences of a
// given key and return the new head of the list
static Node DeleteOccurrences(Node head, int key) {
// Initialize pointers to traverse the linked list
Node curr = head;
Node prev = null;
// Traverse the list to delete all occurrences
while (curr != null) {
// If current node's data is equal to key
if (curr.Data == key) {
// If node to be deleted is head node
if (prev == null) {
head = curr.next;
}
else {
prev.next = curr.next;
}
// Move to the next node
curr = curr.next;
}
else {
// Move pointers one position ahead
prev = curr;
curr = curr.next;
}
}
return head;
}
static void PrintList(Node curr) {
while (curr != null) {
Console.Write(" " + curr.Data);
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Create a hard-coded linked list:
// 2 -> 2 -> 1 -> 8 -> 2 -> NULL
Node head = new Node(2);
head.next = new Node(2);
head.next.next = new Node(1);
head.next.next.next = new Node(8);
head.next.next.next.next = new Node(2);
int key = 2;
head = DeleteOccurrences(head, key);
PrintList(head);
}
}
JavaScript
// JavaScript program to delete all occurrences of a given key
// in a singly linked list
class Node {
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 list
function deleteOccurrences(head, key) {
// Initialize pointers to traverse the linked list
let curr = head;
let prev = null;
// Traverse the list to delete all occurrences
while (curr !== null) {
// If current node's data is equal to key
if (curr.data === key) {
// If node to be deleted is head node
if (prev === null) {
head = curr.next;
}
else {
prev.next = curr.next;
}
// Move to the next node
curr = curr.next;
}
else {
// Move pointers one position ahead
prev = curr;
curr = curr.next;
}
}
return head;
}
function printList(curr) {
while (curr !== null) {
console.log(" " + curr.data);
curr = curr.next;
}
console.log();
}
// Create a hard-coded linked list:
// 2 -> 2 -> 1 -> 8 -> 2 -> NULL
let head = new Node(2);
head.next = new Node(2);
head.next.next = new Node(1);
head.next.next.next = new Node(8);
head.next.next.next.next = new Node(2);
let key = 2;
head = deleteOccurrences(head, key);
printList(head);
Time complexity: O(n), where n is the number of nodes in the list.
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem