Remove all occurrences of key Y after the first occurrence node X in Linked List
Given a Linked List and two integers X and Y, the task is to remove all occurrences of Y after the first occurrence of a node with value X and print the modified linked list.
Examples:
Input: 7 ? 20 ? 9 ? 10 ? 20 ? 14 ? 15 ? 20, X = 10, Y = 20
Output: 7 ? 20 ? 9 ? 10 ? 14 ? 15
Input: LL: 10 ? 20, X = 10, Y = 20
Output: LL: 10
Approach: The given problem can be solved by traversing the given Linked List and deleting all the nodes with value Y occurring after the node with value X. Follow the steps below to solve the problem:
- Initialize two list nodes, K and prev, to store the current head of the given Linked List and the previous node of the current head of the Linked List.
- Traverse the given Linked List until K becomes NULL and performs the following steps:
- Iterate the node K until a node with value X is found and simultaneously update the node prev as the previous node K.
- Store the node K in another variable, say temp, and traverse the Linked List until the node with value Y has occurred and simultaneously update the node prev to the previous node K.
- If the value of temp is NULL, then break out of the loop. Otherwise, update the next pointer of prev to the next pointer of temp and update temp to the next pointer of the node prev.
- After completing the above steps, print the modified Linked List.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
Node( int d)
{
data = d;
next = NULL;
}
};
class Solution {
public :
Node* head = NULL;
void deleteKey( int A, int key)
{
Node *k = head, *prev = NULL;
while (k != NULL) {
while (k != NULL && k->data != A) {
prev = k;
k = k->next;
}
Node* temp = k;
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return ;
prev->next = temp->next;
temp = prev->next;
}
}
void push( int new_data)
{
Node* new_node = new Node(new_data);
new_node->next = head;
head = new_node;
}
void printList()
{
Node* tnode = head;
while (tnode != NULL) {
cout << tnode->data << " " ;
tnode = tnode->next;
}
}
};
int main()
{
Solution obj;
obj.push(20);
obj.push(15);
obj.push(10);
obj.push(20);
obj.push(10);
obj.push(9);
obj.push(20);
obj.push(7);
int X = 10, Y = 20;
obj.deleteKey(X, Y);
obj.printList();
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.util.LinkedList;
class Main {
static Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
void deleteKey( int A, int key)
{
Node k = head, prev = null ;
while (k != null ) {
while (k != null
&& k.data != A) {
prev = k;
k = k.next;
}
Node temp = k;
while (temp != null
&& temp.data != key) {
prev = temp;
temp = temp.next;
}
if (temp == null )
return ;
prev.next = temp.next;
temp = prev.next;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void printList()
{
Node tnode = head;
while (tnode != null ) {
System.out.print(tnode.data + " " );
tnode = tnode.next;
}
}
public static void main(String[] args)
{
Main list = new Main();
list.push( 20 );
list.push( 15 );
list.push( 10 );
list.push( 20 );
list.push( 10 );
list.push( 9 );
list.push( 20 );
list.push( 7 );
int X = 10 ;
int Y = 20 ;
list.deleteKey(X, Y);
list.printList();
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self .left = None
self .right = None
def deleteKey(head, A, key):
k, prev = head, None
while (k ! = None ):
while (k ! = None and k.data ! = A):
prev = k
k = k. next
temp = k
while (temp ! = None and temp.data ! = key):
prev = temp
temp = temp. next
if (temp = = None ):
return
prev. next = temp. next
temp = prev. next
return head
def push(head, new_data):
new_node = Node(new_data)
new_node. next = head
head = new_node
return head
def printList(head):
tnode = head
while (tnode. next ! = None ):
print (tnode.data, end = " " )
tnode = tnode. next
if __name__ = = '__main__' :
list = None
list = push( list , 20 )
list = push( list , 15 )
list = push( list , 10 )
list = push( list , 20 )
list = push( list , 10 )
list = push( list , 9 )
list = push( list , 20 )
list = push( list , 7 )
X = 10
Y = 20
list = deleteKey( list , X, Y)
printList( list )
|
C#
using System;
class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
class GFG{
static Node head;
void deleteKey( int A, int key)
{
Node k = head, prev = null ;
while (k != null )
{
while (k != null && k.data != A)
{
prev = k;
k = k.next;
}
Node temp = k;
while (temp != null &&
temp.data != key)
{
prev = temp;
temp = temp.next;
}
if (temp == null )
return ;
prev.next = temp.next;
temp = prev.next;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
public void printList()
{
Node tnode = head;
while (tnode != null )
{
Console.Write(tnode.data + " " );
tnode = tnode.next;
}
}
static public void Main()
{
GFG list = new GFG();
list.push(20);
list.push(15);
list.push(10);
list.push(20);
list.push(10);
list.push(9);
list.push(20);
list.push(7);
int X = 10;
int Y = 20;
list.deleteKey(X, Y);
list.printList();
}
}
|
Javascript
<script>
var head;
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function deleteKey(A , key) {
var k = head, prev = null ;
while (k != null ) {
while (k != null && k.data != A) {
prev = k;
k = k.next;
}
var temp = k;
while (temp != null && temp.data != key) {
prev = temp;
temp = temp.next;
}
if (temp == null )
return ;
prev.next = temp.next;
temp = prev.next;
}
}
function push(new_data) {
var new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
function printList() {
var tnode = head;
while (tnode != null ) {
document.write(tnode.data + " " );
tnode = tnode.next;
}
}
push(20);
push(15);
push(10);
push(20);
push(10);
push(9);
push(20);
push(7);
var X = 10;
var Y = 20;
deleteKey(X, Y);
printList();
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
23 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...