Skip to content
Related Articles

Related Articles

Improve Article

Delete all the nodes from the doubly linked list that are greater than a given value

  • Difficulty Level : Basic
  • Last Updated : 06 Aug, 2021

Given a doubly linked list containing N nodes and a number X, the task is to delete all the nodes from the list that are greater than the given value X.
Examples: 
 

Input: 10 8 4 11 9, X = 9 
Output: 8 4 9 
Explanation:  10 and 11 are greater than 9. So after removing them doubly linked list will become 8 4 9.
Input: 4 4 2 1 3 5, X = 2 
Output: 2 1

 

Approach: Traverse the nodes of the doubly linked list one by one and get the pointer of the nodes having data value greater than x. Delete these nodes by following the approach used in this post.
 

C++




// C++ implementation to delete all
// the nodes from the doubly
// linked list that are greater than
// the specified value x
#include <bits/stdc++.h>
 
using namespace std;
 
// Node of the doubly linked list
struct Node {
    int data;
    Node *prev, *next;
};
 
// function to insert a node at the beginning
// of the Doubly Linked List
void push(Node** head_ref, int new_data)
{
    // allocate node
    Node* new_node = (Node*)malloc(sizeof(struct Node));
 
    // put in the data
    new_node->data = new_data;
 
    // since we are adding at the beginning,
    // prev is always NULL
    new_node->prev = NULL;
 
    // link the old list off the new node
    new_node->next = (*head_ref);
 
    // change prev of head node to new node
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
 
    // move the head to point to the new node
    (*head_ref) = new_node;
}
 
// function to delete a node in a Doubly Linked List.
// head_ref --> pointer to head node pointer.
// del  -->  pointer to node to be deleted
void deleteNode(Node** head_ref, Node* del)
{
    // base case
    if (*head_ref == NULL || del == NULL)
        return;
 
    // If node to be deleted is head node
    if (*head_ref == del)
        *head_ref = del->next;
 
    // Change next only if node to be
    // deleted is NOT the last node
    if (del->next != NULL)
        del->next->prev = del->prev;
 
    // Change prev only if node to be
    // deleted is NOT the first node
    if (del->prev != NULL)
        del->prev->next = del->next;
 
    // Finally, free the memory occupied by del
    free(del);
 
    return;
}
 
// function to delete all the nodes
// from the doubly linked
// list that are greater than the
// specified value x
void deleteGreaterNodes(Node** head_ref, int x)
{
    Node* ptr = *head_ref;
    Node* next;
 
    while (ptr != NULL) {
        next = ptr->next;
        // if true, delete node 'ptr'
        if (ptr->data > x)
            deleteNode(head_ref, ptr);
        ptr = next;
    }
}
 
// function to print nodes in a
// given doubly linked list
void printList(Node* head)
{
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
 
// Driver program to test above
int main()
{
    // start with the empty list
    Node* head = NULL;
 
    // create the doubly linked list
    // 10<->8<->4<->11<->9
    push(&head, 9);
    push(&head, 11);
    push(&head, 4);
    push(&head, 8);
    push(&head, 10);
 
    int x = 9;
 
    cout << "Original List: ";
    printList(head);
 
    deleteGreaterNodes(&head, x);
 
    cout << "\nModified List: ";
    printList(head);
}

Java




// Java implementation to delete all
// the nodes from the doubly
// linked list that are greater than
// the specified value x
class GFG
{
 
// Node of the doubly linked list
static class Node
{
    int data;
    Node prev, next;
};
 
// function to insert a node at the beginning
// of the Doubly Linked List
static Node push(Node head_ref, int new_data)
{
    // allocate node
    Node new_node = new Node();
 
    // put in the data
    new_node.data = new_data;
 
    // since we are adding at the beginning,
    // prev is always null
    new_node.prev = null;
 
    // link the old list off the new node
    new_node.next = (head_ref);
 
    // change prev of head node to new node
    if ((head_ref) != null)
        (head_ref).prev = new_node;
 
    // move the head to point to the new node
    (head_ref) = new_node;
     
    return head_ref;
}
 
// function to delete a node in a Doubly Linked List.
// head_ref -. pointer to head node pointer.
// del -. pointer to node to be deleted
static Node deleteNode(Node head_ref, Node del)
{
    // base case
    if (head_ref == null || del == null)
        return null;
 
    // If node to be deleted is head node
    if (head_ref == del)
        head_ref = del.next;
 
    // Change next only if node to be
    // deleted is NOT the last node
    if (del.next != null)
        del.next.prev = del.prev;
 
    // Change prev only if node to be
    // deleted is NOT the first node
    if (del.prev != null)
        del.prev.next = del.next;
 
    return head_ref;
}
 
// function to delete all the nodes
// from the doubly linked
// list that are greater than the
// specified value x
static Node deleteGreaterNodes(Node head_ref, int x)
{
    Node ptr = head_ref;
    Node next;
 
    while (ptr != null)
    {
        next = ptr.next;
         
        // if true, delete node 'ptr'
        if (ptr.data > x)
            deleteNode(head_ref, ptr);
        ptr = next;
    }
    return head_ref;
}
 
// function to print nodes in a
// given doubly linked list
static void printList(Node head)
{
    while (head != null)
    {
        System.out.print( head.data + " ");
        head = head.next;
    }
}
 
// Driver code
public static void main(String args[])
{
    // start with the empty list
    Node head = null;
 
    // create the doubly linked list
    // 10<.8<.4<.11<.9
    head = push(head, 9);
    head = push(head, 11);
    head = push(head, 4);
    head = push(head, 8);
    head = push(head, 10);
 
    int x = 9;
 
    System.out.print( "Original List: ");
    printList(head);
 
    head=deleteGreaterNodes(head, x);
 
    System.out.print( "\nModified List: ");
    printList(head);
}
}
 
// This code is contributed by Arnab Kundu

Python




# Python implementation to delete all
# the nodes from the doubly
# linked list that are greater than
# the specified value x
 
# Link list node
class Node:
     
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None
         
# function to insert a node at the beginning
# of the Doubly Linked List
def push( head_ref, new_data):
 
    # allocate node
    new_node = Node(0)
 
    # put in the data
    new_node.data = new_data
 
    # since we are adding at the beginning,
    # prev is always None
    new_node.prev = None
 
    # link the old list off the new node
    new_node.next = (head_ref)
 
    # change prev of head node to new node
    if ((head_ref) != None) :
        (head_ref).prev = new_node
 
    # move the head to point to the new node
    (head_ref) = new_node
     
    return head_ref;
 
# function to delete a node in a Doubly Linked List.
# head_ref -. pointer to head node pointer.
# del -. pointer to node to be deleted
def deleteNode(head_ref, del_) :
 
    # base case
    if (head_ref == None or del_ == None) :
        return head_ref
 
    # If node to be deleted is head node
    if (head_ref == del_) :
        head_ref = del_.next
 
    # Change next only if node to be
    # deleted is NOT the last node
    if (del_.next != None) :
        del_.next.prev = del_.prev
 
    # Change prev only if node to be
    # deleted is NOT the first node
    if (del_.prev != None) :
        del_.prev.next = del_.next
 
    return head_ref
 
# function to delete all the nodes
# from the doubly linked
# list that are greater than the
# specified value x
def deleteGreaterNodes(head_ref, x) :
 
    ptr = head_ref
    next = None
 
    while (ptr != None) :
        next = ptr.next
         
        # if true, delete node 'ptr'
        if (ptr.data > x) :
            head_ref = deleteNode(head_ref, ptr)
        ptr = next
         
    return head_ref;
 
# function to print nodes in a
# given doubly linked list
def printList( head) :
 
    while (head != None) :
        print( head.data, end= " ")
        head = head.next
 
# Driver program to test above
 
# start with the empty list
head = None
 
# create the doubly linked list
# 10<.8<.4<.11<.9
head = push(head, 9)
head = push(head, 11)
head = push(head, 4)
head = push(head, 8)
head = push(head, 10)
 
x = 9
 
print("Original List: ")
printList(head)
 
head = deleteGreaterNodes(head, x)
 
print("\nModified List: ")
printList(head)
 
# This code is contributed by Arnab Kundu

C#




// C# implementation to delete all
// the nodes from the doubly
// linked list that are greater than
// the specified value x
using System;
 
class GFG
{
  
// Node of the doubly linked list
public class Node
{
    public int data;
    public Node prev, next;
};
  
// function to insert a node at the beginning
// of the Doubly Linked List
static Node push(Node head_ref, int new_data)
{
    // allocate node
    Node new_node = new Node();
  
    // put in the data
    new_node.data = new_data;
  
    // since we are adding at the beginning,
    // prev is always null
    new_node.prev = null;
  
    // link the old list off the new node
    new_node.next = (head_ref);
  
    // change prev of head node to new node
    if ((head_ref) != null)
        (head_ref).prev = new_node;
  
    // move the head to point to the new node
    (head_ref) = new_node;
      
    return head_ref;
}
  
// function to delete a node in a Doubly Linked List.
// head_ref -. pointer to head node pointer.
// del -. pointer to node to be deleted
static Node deleteNode(Node head_ref, Node del)
{
    // base case
    if (head_ref == null || del == null)
        return null;
  
    // If node to be deleted is head node
    if (head_ref == del)
        head_ref = del.next;
  
    // Change next only if node to be
    // deleted is NOT the last node
    if (del.next != null)
        del.next.prev = del.prev;
  
    // Change prev only if node to be
    // deleted is NOT the first node
    if (del.prev != null)
        del.prev.next = del.next;
  
    return head_ref;
}
  
// function to delete all the nodes
// from the doubly linked
// list that are greater than the
// specified value x
static Node deleteGreaterNodes(Node head_ref, int x)
{
    Node ptr = head_ref;
    Node next;
  
    while (ptr != null)
    {
        next = ptr.next;
          
        // if true, delete node 'ptr'
        if (ptr.data > x)
            deleteNode(head_ref, ptr);
        ptr = next;
    }
    return head_ref;
}
  
// function to print nodes in a
// given doubly linked list
static void printList(Node head)
{
    while (head != null)
    {
        Console.Write( head.data + " ");
        head = head.next;
    }
}
  
// Driver code
public static void Main(String []args)
{
    // start with the empty list
    Node head = null;
  
    // create the doubly linked list
    // 10<.8<.4<.11<.9
    head = push(head, 9);
    head = push(head, 11);
    head = push(head, 4);
    head = push(head, 8);
    head = push(head, 10);
  
    int x = 9;
  
    Console.Write( "Original List: ");
    printList(head);
  
    head=deleteGreaterNodes(head, x);
  
    Console.Write( "\nModified List: ");
    printList(head);
}
}
 
// This code contributed by Rajput-Ji

Javascript




<script>
// Javascript implementation to delete all
// the nodes from the doubly
// linked list that are greater than
// the specified value x
 
// Node of the doubly linked list
class Node
{
    constructor()
    {
        this.prev = null;
        this.next = null;
        this.data = 0;
    }
}
 
// function to insert a node at the beginning
// of the Doubly Linked List
function push(head_ref,new_data)
{
    // allocate node
    let new_node = new Node();
  
    // put in the data
    new_node.data = new_data;
  
    // since we are adding at the beginning,
    // prev is always null
    new_node.prev = null;
  
    // link the old list off the new node
    new_node.next = (head_ref);
  
    // change prev of head node to new node
    if ((head_ref) != null)
        (head_ref).prev = new_node;
  
    // move the head to point to the new node
    (head_ref) = new_node;
      
    return head_ref;
}
 
// function to delete a node in a Doubly Linked List.
// head_ref -. pointer to head node pointer.
// del -. pointer to node to be deleted
function deleteNode(head_ref,del)
{
    // base case
    if (head_ref == null || del == null)
        return null;
  
    // If node to be deleted is head node
    if (head_ref == del)
        head_ref = del.next;
  
    // Change next only if node to be
    // deleted is NOT the last node
    if (del.next != null)
        del.next.prev = del.prev;
  
    // Change prev only if node to be
    // deleted is NOT the first node
    if (del.prev != null)
        del.prev.next = del.next;
  
    return head_ref;
}
 
// function to delete all the nodes
// from the doubly linked
// list that are greater than the
// specified value x
function deleteGreaterNodes(head_ref,x)
{
    let ptr = head_ref;
    let next;
  
    while (ptr != null)
    {
        next = ptr.next;
          
        // if true, delete node 'ptr'
        if (ptr.data > x)
            deleteNode(head_ref, ptr);
        ptr = next;
    }
    return head_ref;
}
 
// function to print nodes in a
// given doubly linked list
function printList(head)
{
    while (head != null)
    {
        document.write( head.data + " ");
        head = head.next;
    }
}
 
// Driver code
// start with the empty list
let head = null;
 
// create the doubly linked list
// 10<.8<.4<.11<.9
head = push(head, 9);
head = push(head, 11);
head = push(head, 4);
head = push(head, 8);
head = push(head, 10);
 
let x = 9;
 
document.write( "Original List: ");
printList(head);
 
head=deleteGreaterNodes(head, x);
 
document.write( "<br>Modified List: ");
printList(head);
 
// This code is contributed by avanitrachhadiya2155
</script>
Output: 



Original List: 10 8 4 11 9 
Modified List: 8 4 9

 

Time Complexity: O(N)
Auxiliary Space: O(1) 
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :