Delete all the nodes from the list that are greater than x
Last Updated :
07 Nov, 2023
Given a linked list, the problem is to delete all the nodes from the list that are greater than the specified value x.
Examples:
Input : list: 7->3->4->8->5->1
x = 6
Output : 3->4->5->1
Input : list: 1->8->7->3->7->10
x = 7
Output : 1->7->3->7
Source: Microsoft Interview Experience | Set 169.
Approach: This is mainly a variation of the post which deletes first occurrence of a given key. We need to first check for all occurrences at head node which are greater than ‘x’, delete them and change the head node appropriately. Then we need to check for all occurrences inside a loop and delete them one by one.
Implementation:
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 deleteGreaterNodes(Node** head_ref, int x)
{
Node *temp = *head_ref, *prev;
if (temp != NULL && temp->data > x) {
*head_ref = temp->next;
free (temp);
temp = *head_ref;
}
while (temp != NULL) {
while (temp != NULL && temp->data <= x) {
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(7);
head->next = getNode(3);
head->next->next = getNode(4);
head->next->next->next = getNode(8);
head->next->next->next->next = getNode(5);
head->next->next->next->next->next = getNode(1);
int x = 6;
cout << "Original List: " ;
printList(head);
deleteGreaterNodes(&head, x);
cout << "\nModified List: " ;
printList(head);
return 0;
}
|
Java
import java.io.*;
class Node {
int data;
Node next;
}
class GFG {
public Node getNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
public void printList(Node head)
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
}
public Node deleteGreaterNodes(Node head, int x)
{
Node temp = head;
while (temp != null && temp.data > x) {
temp = temp.next;
head = temp;
}
temp = head;
Node prev = temp;
while (temp != null ) {
while (temp != null && temp.data <= x) {
prev = temp;
temp = temp.next;
}
if (temp == null ) {
return head;
}
prev.next = temp.next;
temp = prev.next;
}
return head;
}
public static void main(String[] args)
{
GFG list = new GFG();
Node head = list.getNode( 7 );
head.next = list.getNode( 3 );
head.next.next = list.getNode( 4 );
head.next.next.next = list.getNode( 8 );
head.next.next.next.next = list.getNode( 5 );
head.next.next.next.next.next = list.getNode( 1 );
System.out.print( "Original List: " );
list.printList(head);
int x = 6 ;
head = list.deleteGreaterNodes(head, x);
System.out.print( "\nModified List: " );
list.printList(head);
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self . next = None
def getNode(data):
return Node(data)
def deleteGreaterNodes(head_ref, x):
temp = head_ref
prev = None
while (temp is not None and temp.data > x):
head_ref = temp. next
temp = head_ref
while (temp is not None ):
while (temp is not None and temp.data < = x):
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( 7 )
head. next = getNode( 3 )
head. next . next = getNode( 4 )
head. next . next . next = getNode( 8 )
head. next . next . next . next = getNode( 5 )
head. next . next . next . next . next = getNode( 1 )
x = 6
print ( "Original List: " )
printList(head)
head = deleteGreaterNodes(head, x)
print ( "Modified List: " )
printList(head)
|
C#
using System;
public class Node
{
public int Data;
public Node Next;
public Node( int data)
{
Data = data;
Next = null ;
}
}
public class LinkedList
{
public static void DeleteGreaterNodes( ref Node head, int x)
{
Node temp = head;
Node prev = null ;
if (temp != null && temp.Data > x)
{
head = temp.Next;
temp = null ;
temp = head;
}
while (temp != null )
{
while (temp != null && temp.Data <= x)
{
prev = temp;
temp = temp.Next;
}
if (temp == null )
return ;
prev.Next = temp.Next;
temp = null ;
temp = prev.Next;
}
}
public static void PrintList(Node head)
{
while (head != null )
{
Console.Write(head.Data + " " );
head = head.Next;
}
}
public static void Main( string [] args)
{
Node head = new Node(7);
head.Next = new Node(3);
head.Next.Next = new Node(4);
head.Next.Next.Next = new Node(8);
head.Next.Next.Next.Next = new Node(5);
head.Next.Next.Next.Next.Next = new Node(1);
int x = 6;
Console.Write( "Original List: " );
PrintList(head);
DeleteGreaterNodes( ref head, x);
Console.Write( "\nModified 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 deleteGreaterNodes(head_ref, x){
let temp = head_ref;
let prev;
if (temp != null && temp.data > x){
head_ref = temp.next;
temp = head_ref;
}
while (temp != null ){
while (temp != null && temp.data <= x){
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(7);
head.next = getNode(3);
head.next.next = getNode(4);
head.next.next.next = getNode(8);
head.next.next.next.next = getNode(5);
head.next.next.next.next.next = getNode(1);
let x = 6;
console.log( "Original List:" );
printList(head);
head = deleteGreaterNodes(head, x);
console.log( "Modified List:" );
printList(head);
|
Output
Original List: 7 3 4 8 5 1
Modified List: 3 4 5 1
Time Complexity: O(n).
Using Recursion:
The idea is to traverse the linked list recursively and delete all nodes whose value is greater than x. We start the traversal from the head of the linked list and move to the next node until we reach the end of the list.
- Check if the head of the linked list is NULL. If it is, return NULL.
- Recursively traverse the sub-list starting from head’s next pointer and delete all nodes greater than x.
- Check if head’s value is greater than x. If it is, delete the node and return its next pointer to the previous level of the recursion.
- Otherwise, simply return the node itself.
- Repeat steps 2-4 for all nodes in the linked list.
- Return the head of the modified linked list.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class Node {
public :
int data;
Node* next;
Node( int value)
{
data = value;
next = NULL;
}
};
Node* deleteGreaterNodes(Node* head, int x)
{
if (head == NULL) {
return NULL;
}
head->next = deleteGreaterNodes(
head->next, x);
if (head->data
> x) {
Node* temp = head->next;
delete head;
return temp;
}
return head;
}
void printList(Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
cout << endl;
}
int main()
{
Node* head = new Node(7);
head->next = new Node(3);
head->next->next = new Node(4);
head->next->next->next = new Node(8);
head->next->next->next->next = new Node(5);
head->next->next->next->next->next = new Node(1);
int x = 6;
cout << "Original List: " ;
printList(head);
head = deleteGreaterNodes(head, x);
cout << "List after deleting nodes greater than " << x
<< ": " ;
printList(head);
return 0;
}
|
Java
class Node {
int data;
Node next;
Node( int value) {
data = value;
next = null ;
}
}
public class Main {
static Node deleteGreaterNodes(Node head, int x) {
if (head == null ) {
return null ;
}
head.next = deleteGreaterNodes(
head.next, x);
if (head.data > x) {
Node temp = head.next;
head = null ;
return temp;
}
return head;
}
static void printList(Node head) {
while (head != null ) {
System.out.print(head.data + " " );
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = new Node( 7 );
head.next = new Node( 3 );
head.next.next = new Node( 4 );
head.next.next.next = new Node( 8 );
head.next.next.next.next = new Node( 5 );
head.next.next.next.next.next = new Node( 1 );
int x = 6 ;
System.out.print( "Original List: " );
printList(head);
head = deleteGreaterNodes(head, x);
System.out.print( "List after deleting nodes greater than " + x + ": " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , value):
self .data = value
self . next = None
def deleteGreaterNodes(head, x):
if head is None :
return None
head. next = deleteGreaterNodes(head. next , x)
if head.data > x:
temp = head. next
del head
return temp
return head
def printList(head):
while head is not None :
print (head.data, end = " " )
head = head. next
print ()
if __name__ = = "__main__" :
head = Node( 7 )
head. next = Node( 3 )
head. next . next = Node( 4 )
head. next . next . next = Node( 8 )
head. next . next . next . next = Node( 5 )
head. next . next . next . next . next = Node( 1 )
x = 6
print ( "Original List: " , end = "")
printList(head)
head = deleteGreaterNodes(head, x)
print (f "List after deleting nodes greater than {x}: " , end = "")
printList(head)
|
C#
using System;
public class Node
{
public int data;
public Node next;
public Node( int value)
{
data = value;
next = null ;
}
}
public class GFG
{
public static Node DeleteGreaterNodes(Node head, int x)
{
if (head == null )
{
return null ;
}
head.next = DeleteGreaterNodes(head.next, x);
if (head.data > x)
{
Node temp = head.next;
head.next = null ;
return temp;
}
return head;
}
public static void PrintList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
Console.WriteLine();
}
public static void Main()
{
Node head = new Node(7);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(8);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(1);
int x = 6;
Console.Write( "Original List: " );
PrintList(head);
head = DeleteGreaterNodes(head, x);
Console.Write( "List after deleting nodes greater than " + x + ": " );
PrintList(head);
}
}
|
Javascript
class Node {
constructor(value) {
this .data = value;
this .next = null ;
}
}
function deleteGreaterNodes(head, x) {
if (head === null ) {
return null ;
}
head.next = deleteGreaterNodes(head.next, x);
if (head.data > x) {
const temp = head.next;
head = null ;
return temp;
}
return head;
}
function printList(head) {
let current = head;
while (current !== null ) {
console.log(current.data + " " );
current = current.next;
}
console.log();
}
const head = new Node(7);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(8);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(1);
const x = 6;
console.log( "Original List: " );
printList(head);
const newHead = deleteGreaterNodes(head, x);
console.log( "List after deleting nodes greater than " + x + ": " );
printList(newHead);
|
Output
Original List: 7 3 4 8 5 1
List after deleting nodes greater than 6: 3 4 5 1
Time Complexity: O(n) where n is the number of nodes in the linked list.
Auxiliary Space: O(n), due to the recursive call stack.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...