Delete linked list nodes which have a Lesser Value on Left Side
Last Updated :
29 Aug, 2022
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->3
Output: Modified Linked List = 12 -> 10 -> 5 -> 2
Input: 25->15->6->48->12->5->16->14
Output: Modified Linked List = 25 -> 15 -> 6 -> 5
Approach:
- Initialise a variable maximum with head node.
- Traverse the list.
- Check if the next node is lesser than max_node. If yes, move to the next node.
- Else If not, then update the value of max_node and delete the next node.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void delNodes( struct Node* head)
{
struct Node* current = head;
struct Node* maxnode = head;
struct Node* temp;
while (current != NULL && current->next != NULL) {
if (current->next->data <= maxnode->data) {
current = current->next;
maxnode = current;
}
else {
temp = current->next;
current->next = temp->next;
free (temp);
}
}
}
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;
}
void printList( struct Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
cout << endl;
}
int main()
{
struct Node* head = NULL;
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: " );
printList(head);
delNodes(head);
printf ( "\nModified Linked List: " );
printList(head);
return 0;
}
|
Java
class Solution
{
static class Node
{
int data;
Node next;
};
static Node delNodes(Node head)
{
Node current = head;
Node maxnode = head;
Node temp;
while (current != null && current.next != null )
{
if (current.next.data <= maxnode.data)
{
current = current.next;
maxnode = current;
}
else
{
temp = current.next;
current.next = temp.next;
}
}
return head;
}
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;
}
static Node printList(Node head)
{
while (head != null )
{
System.out.print( head.data + " " );
head = head.next;
}
System.out.println();
return head;
}
public static void main(String args[])
{
Node head = null ;
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);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def delNodes(head):
current = head
maxnode = head
temp = None
while (current ! = None and current. next ! = None ):
if (current. next .data < = maxnode.data):
current = current. next
maxnode = current
else :
temp = current. next
current. next = temp. next
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
def printList(head):
while (head ! = None ) :
print (head.data, end = " " )
head = head. next
print ()
if __name__ = = '__main__' :
head = None
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: " , end = "")
printList(head)
delNodes(head)
print ( "\nModified Linked List: " , end = "")
printList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node delNodes(Node head)
{
Node current = head;
Node maxnode = head;
Node temp;
while (current != null &&
current.next != null )
{
if (current.next.data <= maxnode.data)
{
current = current.next;
maxnode = current;
}
else
{
temp = current.next;
current.next = temp.next;
}
}
return head;
}
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;
}
static Node printList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
Console.WriteLine();
return head;
}
public static void Main(String []args)
{
Node head = null ;
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);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function delNodes(head) {
var current = head;
var maxnode = head;
var temp;
while (current != null && current.next != null ) {
if (current.next.data <= maxnode.data) {
current = current.next;
maxnode = current;
}
else {
temp = current.next;
current.next = temp.next;
}
}
return head;
}
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;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
document.write();
return head;
}
var head = null ;
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 " );
printList(head);
head = delNodes(head);
document.write( "<br/><br/>Modified Linked List " );
printList(head);
</script>
|
Output:
Given Linked List: 12 15 10 11 5 6 2 3
Modified Linked List: 12 10 5 2
Time Complexity: O(N), since one traversal of the linked List is required to complete all operations hence the overall time required by the algorithm is linear
Auxiliary Space: O(1) , since no extra array is used the space taken by the algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...