Delete all the nodes from the list which are less than K
Last Updated :
19 Apr, 2023
Given a Linked List and a key K. The task is to write a program to delete all the nodes from the list that are lesser than the key K.
Examples:
Input :12->15->9->11->5->6
K = 9
Output : 12 -> 15 -> 9 -> 11
Input : 13->4->16->9->22->45->5->16->6
K = 10
Output : 13 -> 16 -> 22 -> 45 -> 16
Approach: The approach is similar to deleting all nodes from the list which are greater than the given key.
There are two possible cases:
- Head node holds a value less than K: First check for all occurrences at head node which are lesser than ‘K’, delete them and change the head node appropriately.
- Some intermediate node holds value less than k: Start traversing from head and check if current node’s value is less than K. If yes then delete that node and move forward in the list.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void deleteLesserNodes(Node** head_ref, int K)
{
Node *temp = *head_ref, *prev;
while (temp != NULL && temp->data < K) {
*head_ref = temp->next;
free (temp);
temp = *head_ref;
}
while (temp != NULL) {
while (temp != NULL && temp->data >= K) {
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return ;
prev->next = temp->next;
delete temp;
temp = prev->next;
}
}
void printList(Node* head)
{
while (head) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
Node* head = getNode(12);
head->next = getNode(15);
head->next->next = getNode(9);
head->next->next->next = getNode(11);
head->next->next->next->next = getNode(5);
head->next->next->next->next->next = getNode(6);
int K = 9;
cout << "Initial List: " ;
printList(head);
deleteLesserNodes(&head, K);
cout << "\nFinal List: " ;
printList(head);
return 0;
}
|
Java
import java.io.*;
class Node{
int data;
Node next;
public Node( int data){
this .data = data;
this .next = null ;
}
}
public class GFG {
public static Node deleteLesserNodes(Node head_ref, int K){
Node temp = head_ref;
Node prev = null ;
while (temp != null && temp.data < K){
head_ref = temp.next;
temp = head_ref;
}
while (temp != null ){
while (temp != null && temp.data >= K){
prev = temp;
temp = temp.next;
}
if (temp == null )
return head_ref;
prev.next = temp.next;
temp = prev.next;
}
return head_ref;
}
public static void printList(Node head){
while (head != null ){
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String[] args) {
Node head = new Node( 12 );
head.next = new Node( 15 );
head.next.next = new Node( 9 );
head.next.next.next = new Node( 11 );
head.next.next.next.next = new Node( 5 );
head.next.next.next.next.next = new Node( 6 );
int K = 9 ;
System.out.println( "Initial List:" );
printList(head);
head = deleteLesserNodes(head, K);
System.out.println( "\nFinal List:" );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self . next = None
def getNode(data):
return Node(data)
def deleteLesserNodes(head_ref, K):
temp = head_ref
prev = None
while (temp is not None and temp.data < K):
head_ref = temp. next
temp = head_ref
while (temp is not None ):
while (temp is not None and temp.data > = K):
prev = temp
temp = temp. next
if (temp is None ):
return head_ref
prev. next = temp. next
temp = prev. next
return head_ref
def printList(head):
while (head is not None ):
print (head.data, end = " " )
head = head. next
print ( "\n" )
head = getNode( 12 )
head. next = getNode( 15 )
head. next . next = getNode( 9 )
head. next . next . next = getNode( 11 )
head. next . next . next . next = getNode( 5 )
head. next . next . next . next . next = getNode( 6 )
K = 9
print ( "Initial List: " )
printList(head)
head = deleteLesserNodes(head, K)
print ( "Final List: " )
printList(head)
|
C#
using System;
class Node
{
public int data;
public Node next;
public Node( int data)
{
this .data = data;
next = null ;
}
}
class LinkedList
{
public static void DeleteLesserNodes( ref Node head_ref, int K)
{
Node temp = head_ref, prev = null ;
while (temp != null && temp.data < K)
{
head_ref = temp.next;
temp = head_ref;
}
while (temp != null )
{
while (temp != null && temp.data >= K)
{
prev = temp;
temp = temp.next;
}
if (temp == null )
return ;
prev.next = temp.next;
temp = temp.next;
}
}
public static void PrintList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main()
{
Node head = new Node(12);
head.next = new Node(15);
head.next.next = new Node(9);
head.next.next.next = new Node(11);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
int K = 9;
Console.Write( "Initial List: " );
PrintList(head);
DeleteLesserNodes( ref head, K);
Console.Write( "\nFinal List: " );
PrintList(head);
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .next = null ;
}
}
function getNode(data){
let newNode = new Node(data);
return newNode;
}
function deleteLesserNodes(head_ref, K){
let temp = head_ref;
let prev;
while (temp != null && temp.data < K){
head_ref = temp.next;
temp = head_ref;
}
while (temp != null ){
while (temp != null && temp.data >= K){
prev = temp;
temp = temp.next;
}
if (temp == null )
return head_ref;
prev.next = temp.next;
temp = prev.next;
}
return head_ref;
}
function printList(head){
while (head != null ){
console.log(head.data + " " );
head = head.next;
}
}
let head = getNode(12);
head.next = getNode(15);
head.next.next = getNode(9);
head.next.next.next = getNode(11);
head.next.next.next.next = getNode(5);
head.next.next.next.next.next = getNode(6);
let K = 9;
console.log( "Initial List:" );
printList(head);
head = deleteLesserNodes(head, K);
console.log( "Final List:" );
printList(head);
|
Output:
Initial List: 12 15 9 11 5 6
Final List: 12 15 9 11
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...