Delete linked list nodes which have a greater value on left side Last Updated : 02 Sep, 2022 Comments Improve Suggest changes Like Article Like Report Given a singly linked list, the task is to remove all the nodes which have a greater value on the left side. Examples: Input: 12->15->10->11->5->6->2->3Output: Modified Linked List = 12 15 Input: 25->15->6->48->12->5->16->14Output: Modified Linked List = 14 16 48 Approach: Initialize the maximum with head node.Traverse the list.Check if the next node is greater than max_node then update the value of max_node and move to the next node.Else delete the next node. Implementation: C++ // C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Structure of a linked list node struct Node { int data; struct Node* next; }; // Function to Delete nodes which have // greater value node(s) on right side void delNodes(struct Node* head) { struct Node* current = head; // Initialize max struct Node* maxnode = head; struct Node* temp; while (current != NULL && current->next != NULL) { // If current is greater than max, // then update max and move current if (current->next->data >= maxnode->data) { current = current->next; maxnode = current; } // If current is smaller than max, then delete current else { temp = current->next; current->next = temp->next; free(temp); } } } /* Utility function to insert a node at the beginning */ void push(struct Node** head_ref, int new_data) { struct Node* new_node = new Node; new_node->data = new_data; new_node->next = *head_ref; *head_ref = new_node; } /* Utility function to print a linked list */ void printList(struct Node* head) { while (head != NULL) { cout << head->data << " "; head = head->next; } cout << endl; } /* Driver program to test above functions */ int main() { struct Node* head = NULL; /* Create following linked list 12->15->10->11->5->6->2->3 */ push(&head, 3); push(&head, 2); push(&head, 6); push(&head, 5); push(&head, 11); push(&head, 10); push(&head, 15); push(&head, 12); printf("Given Linked List \n"); printList(head); delNodes(head); printf("Modified Linked List \n"); printList(head); return 0; } Java // Java implementation of above approach class GFG { // Structure of a linked list node static class Node { int data; Node next; }; // Function to Delete nodes which have // greater value node(s) on right side static Node delNodes(Node head) { Node current = head; // Initialize max Node maxnode = head; Node temp; while (current != null && current.next != null) { // If current is greater than max, // then update max and move current if (current.next.data >= maxnode.data) { current = current.next; maxnode = current; } // If current is smaller than // max, then delete current else { temp = current.next; current.next = temp.next; } } return head; } // Utility function to insert // a node at the beginning static Node push(Node head_ref, int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = head_ref; head_ref = new_node; return head_ref; } // Utility function to print a linked list / static Node printList(Node head) { while (head != null) { System.out.print( head.data + " "); head = head.next; } System.out.println(); return head; } // Driver code public static void main(String args[]) { Node head = null; /* Create following linked list 12->15->10->11->5->6->2->3 */ head=push(head, 3); head=push(head, 2); head=push(head, 6); head=push(head, 5); head=push(head, 11); head=push(head, 10); head=push(head, 15); head=push(head, 12); System.out.printf("Given Linked List \n"); printList(head); head=delNodes(head); System.out.printf("Modified Linked List \n"); printList(head); } } // This code is contributed by Arnab Kundu Python3 # Python3 implementation of above approach import math # Structure of a linked list node class Node: def __init__(self, data): self.data = data self.next = None # Function to Delete nodes which have # greater value node(s) on right side def delNodes( head): current = head # Initialize max maxnode = head while (current != None and current.next != None) : # If current is greater than max, # then update max and move current if (current.next.data >= maxnode.data) : current = current.next maxnode = current # If current is smaller than max, # then delete current else: temp = current.next current.next = temp.next #free(temp) return head # Utility function to insert a node # at the beginning def push(head_ref, new_data): new_node = Node(new_data) new_node.data = new_data new_node.next = head_ref head_ref = new_node return head_ref # Utility function to print a linked list def printList( head): while (head != None) : print(head.data, end = " ") head = head.next print() return head # Driver Code if __name__=='__main__': head = None # Create following linked list #12.15.10.11.5.6.2.3 head = push(head, 3) head = push(head, 2) head = push(head, 6) head = push(head, 5) head = push(head, 11) head = push(head, 10) head = push(head, 15) head = push(head, 12) print("Given Linked List") printList(head) head = delNodes(head) print("Modified Linked List") printList(head) # This code is contributed by Srathore C# // C# implementation of the above approach: using System; class GFG { // Structure of a linked list node public class Node { public int data; public Node next; }; // Function to Delete nodes which have // greater value node(s) on right side static Node delNodes(Node head) { Node current = head; // Initialize max Node maxnode = head; Node temp; while (current != null && current.next != null) { // If current is greater than max, // then update max and move current if (current.next.data >= maxnode.data) { current = current.next; maxnode = current; } // If current is smaller than // max, then delete current else { temp = current.next; current.next = temp.next; } } return head; } // Utility function to insert // a node at the beginning static Node push(Node head_ref, int new_data) { Node new_node = new Node(); new_node.data = new_data; new_node.next = head_ref; head_ref = new_node; return head_ref; } // Utility function to print a linked list static Node printList(Node head) { while (head != null) { Console.Write( head.data + " "); head = head.next; } Console.WriteLine(); return head; } // Driver code public static void Main(String []args) { Node head = null; /* Create following linked list 12->15->10->11->5->6->2->3 */ head = push(head, 3); head = push(head, 2); head = push(head, 6); head = push(head, 5); head = push(head, 11); head = push(head, 10); head = push(head, 15); head = push(head, 12); Console.Write("Given Linked List \n"); printList(head); head = delNodes(head); Console.Write("Modified Linked List \n"); printList(head); } } // This code is contributed by PrinciRaj1992 JavaScript <script> // JavaScript implementation of above approach // Structure of a linked list node class Node { constructor(val) { this.data = val; this.next = null; } } // Function to Delete nodes which have // greater value node(s) on right side function delNodes(head) { var current = head; // Initialize max var maxnode = head; var temp; while (current != null && current.next != null) { // If current is greater than max, // then update max and move current if (current.next.data >= maxnode.data) { current = current.next; maxnode = current; } // If current is smaller than // max, then delete current else { temp = current.next; current.next = temp.next; } } return head; } // Utility function to insert // a node at the beginning function push(head_ref , new_data) { var new_node = new Node(); new_node.data = new_data; new_node.next = head_ref; head_ref = new_node; return head_ref; } // Utility function to print a linked list / function printList(head) { while (head != null) { document.write(head.data + " "); head = head.next; } document.write(); return head; } // Driver code var head = null; /* * Create following linked list 12->15->10->11->5->6->2->3 */ head = push(head, 3); head = push(head, 2); head = push(head, 6); head = push(head, 5); head = push(head, 11); head = push(head, 10); head = push(head, 15); head = push(head, 12); document.write("Given Linked List <br/>"); printList(head); head = delNodes(head); document.write("<br/>Modified Linked List <br/>"); printList(head); // This code contributed by umadevi9616 </script> OutputGiven Linked List 12 15 10 11 5 6 2 3 Modified Linked List 12 15 Complexity Analysis: Time Complexity: O(N), for traversing over the linked list the overall time complexity is linear.Auxiliary Space: O(1) because no extra space is required. Comment More infoAdvertise with us Next Article Delete linked list nodes which have a greater value on left side R rajput-ji Follow Improve Article Tags : Linked List Technical Scripter DSA Technical Scripter 2018 Practice Tags : Linked List Similar Reads Delete linked list nodes which have a Lesser Value on Left Side Given a singly linked list, the task is to remove all the nodes which have a lesser value on left side. Examples: Input: 12->15->10->11->5->6->2->3Output: Modified Linked List = 12 -> 10 -> 5 -> 2 Input: 25->15->6->48->12->5->16->14Output: Modified 7 min read Delete nodes which have a greater value on right side Given a singly linked list, the task is to remove all the nodes with any node on their right whose value is greater and return the head of the modified linked list.Examples: Input: head: 12->15->10->11->5->6->2->3Output: 15->11->6->3Explanation: Node with value 12 , 10, 15+ min read Javascript Program To Delete Nodes Which Have A Greater Value On Right Side Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: Input: 12->15->10->11->5->6->2->3->NULL Output: 15->11->6->3->NULL Explanation: 12, 10, 5 and 2 have been deleted because there is a greater value on the right 5 min read Delete nodes which have a greater value on right side using recursion Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: a) The list 12->15->10->11->5->6->2->3->NULL should be changed to 15->11->6->3->NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater va 7 min read Delete alternate nodes of a Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm 14 min read Like