×

Delete nodes from the linked list which have a greater value on the right side

Delete nodes from the linked list which have a greater value on the right side

In this problem, we have given a singly linked list, and we need to remove all the nodes from it which have a greater value on the right side.

Examples:

  • Suppose the list 13 -> 16 -> 9 -> 10 -> 4 -> 6 -> 1 -> 3 -> NULL. This list will be changed to 16 -> 10 -> 6 -> 3 -> NULL. In this, the nodes 13, 9, 4, 1 have been deleted because in linked list there is a greater value on the right side.
  • The list 90 -> 80 -> 70 -> 60 -> 50 -> NULL will not be changed because there is no node which has the greater value on the right side.

Method:

We can do this by using the following steps:

  • Firstly, we will reverse the given linked list.
  • Then, we will traverse the reversed linked list and keep the max node. If the next node is less than the max node, we will delete the next node, otherwise max = next node.
  • Finally, we will reverse the linked list again to maintain the order of the linked list given earlier.

C program to delete the nodes which have a greater value on the right side

 #include<stdio.h>
 #include<stdlib.h>
 struct node
 {
 int info;
 struct node *next;
 };
 struct node *start = NULL;
 // For inserting the elements in the linked list
 void add(int item)
 {
 struct node *t, *p;
 t = (struct node *)malloc( sizeof( struct node ));
 if(start == NULL)
 {
 start = t;
 start -> info = item;
 start -> next = NULL;
 return;
 }
 else
 {
 struct node *p = start;
 while(p -> next != NULL)
 {
 p = p -> next;
 }
 p -> next = t;
 p = p -> next;
 p -> info = item;
 p -> next = NULL;
 }
 }
 // For reversing the nodes of the linked list
 void reverse (struct node * t)
 {
     struct node * curr = t;
     struct node * next = NULL;
     struct node * pre = NULL;
     while(curr != NULL)
     {
         next=curr -> next;
         curr -> next = pre;
         pre = curr;
         curr = next;
    }
    start = pre;
 }
 void deleteLesser(struct node * temp)
 {
             reverse(temp); // For reversing the linked list
             delete(start); // For delete the nodes which have a node with greater value on left side.
             reverse(start); // For reversing the linked list again to maintain the original order of the linked list
 }
 // Function for delete the nodes which have a node with greater value on the left side.
 void delete(struct node * start)
 {
             struct node * curr = start;
             struct node * max = start;
             struct node * temp;
             while( curr != NULL && curr -> next != NULL)
             {
                         if(curr -> next -> info < max -> info) // If the curr is smaller than the max, then delete the curr
                         {
                                     temp = curr -> next;
                                     curr -> next = temp -> next;
                         }
                         else
                         {
                                     curr = curr -> next;
                                     max = curr;
                         }
             }
 }
 // To display the elements of the linked list
 void traverse(struct node * t)
 {
 if(t == NULL)
 {
             printf(" Linked list is empty\n");
                                     }
                                     while(t -> next != NULL)
                                     {
                         printf("%d -> ",t -> info);
                         t = t -> next;
                         }
                         printf("%d\n",t -> info);
 }
 // Driver Function
 int main()
 {
     add(13);
     add(16);
     add(9);
     add(10);
     add(4);
     add(6);
     add(1);
     add(3);
     printf("Given Linked List: \n");
     traverse(start);
     deleteLesser(start);
     printf("Linked List After deletion: \n");
     traverse(start);
     return 0;
 } 

Output:

Delete nodes from the linked list which have a greater value on the right side

Time Complexity: The time complexity of the above method is O(n).


Related Topics

Linear vs Circular Queue: Data Structure

Difference Between Linear and Circular Queue What is Linear Queue? A linear queue is linear data structure which works on first in first out principle. We can say a linear queue is...

3 minutes read.

Find Bridges in a Graph

You have been given a graph. You have to find out the bridges in that graph. Graph may be connected or disconnected. You have to print vertices of particular edge...

4 minutes read.

What is a Spanning Tree in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

5 minutes read.

Detect Loop in Linked List: Data Structure

Detect the Loop in Linked List: In this problem, we will be seeing some technique through which we can detect the loop in linked list. We will discuss each technique...

3 minutes read.

Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and...

5 minutes read.

B Tree vs B + Tree: Data Structure

Difference Between B Tree and B+ Tree What is B Tree? B-Tree is a self-balancing or special type of m-way tree. B-Trees are used mainly in disc access. If we want...

3 minutes read.

B+ Tree in Data Structure

A B-Tree extension called B+ Tree, which enables effective search, insertion, and deletion operations. Both Records and keys can be stored in internal and leaf nodes in a B tree. Contrarily,...

4 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

String Operations in Data Structures

Operations on Strings Reversing the order of words in a sentence Reversing a string is a technique that reverses or alters the order of a given string so that the last character...

9 minutes read.

Linked List Representation of Binary Tree

As we all know, a binary tree has a maximum of two children and helps us manage the info correctly. The word binary itself represents its meaning; we know that...

4 minutes read.

Operations on Queue in Data Structures

A queue is a linear structure where operations are done in a specific sequence. Queues are abstract data structures that are comparable to Stacks. A queue, unlike a stack, is...

8 minutes read.

Implementation of Queue

Implementation of queue: We can implement the queue through the array and linked list. An array is the easiest way to implement the queue. When a queue is created with the...

7 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

Find the fractional (n/kth) node in the linked list

Find the fractional (n/kth) node in the linked list In this problem, we have given a singly linked list and a number k. Here we need to find the (n/k)th element...

2 minutes read.

Convert Sorted List to Binary Search Tree

Implementation // creating the C++ implementation of the following approach: - #include <bits/stdc++.h> using namespace std; /* Create the link list node and see its implementation. */ class L__Nod { public: int record; L__Nod* next; }; /* constructing a new binary...

15 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.