Delete nodes which have a greater value on right side

Given a linked list, Which include integer node values. Our goal is to remove all nodes which value is less than on right side node. This problem is based on linked list traversal and node deletion with specified condition. Lets see few examples to understand this problem.

Example
----------
    List    1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
        -------------------------------------------
            1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
            1 < 2 (Yes) Delete node 1

            2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
            2 < 5 (Yes) Delete node 2

            5 → 4 → 3 → 6 → 7 → 8 → NULL
            5 < 4 (No)   

            5 → 4 → 3 → 6 → 7 → 8 → NULL
                4 < 3 (No)  

            5 → 4 → 3 → 6 → 7 → 8 → NULL
                    3 < 6 (Yes) Delete node 3  
          
            5 → 4 → 6 → 7 → 8 → NULL   
                    6 < 7 (Yes)

            5 → 4 → 7 → 8 → NULL   
                    7 < 8 (Yes) Delete node 7

            5 → 4 → 8 → NULL
        -------------------------------------------
Output :    5 → 4 → 8 → NULL

Here given code implementation process.

/*
    C program for
    Delete nodes which have a greater value on right side
*/
#include <stdio.h>
#include <stdlib.h>

// Linked List LinkNode
struct LinkNode
{
	int data;
	struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
	struct LinkNode *head;
	struct LinkNode *tail;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
	// Create memory of head and tail Nodes
	struct SingleLL *sll = 
      (struct SingleLL *) malloc(sizeof(struct SingleLL));
	if (sll == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		sll->head = NULL;
		sll->tail = NULL;
	}
	return sll;
}
// Add new Node at end of linked list 
void appendNode(struct SingleLL *sll, struct LinkNode *node)
{
	if (sll->head == NULL)
	{
		sll->head = node;
	}
	else
	{
		// Append the node at last position
		sll->tail->next = node;
	}
	sll->tail = node;
}
// Handles the request of adding new node in linked list
void addNode(struct SingleLL *sll, int data)
{
	// Create dynamic node
	struct LinkNode *node = 
      (struct LinkNode *) malloc(sizeof(struct LinkNode));
	if (node == NULL)
	{
		printf("Memory overflow to Create LinkNode\n");
		return;
	}
	else
	{
		// Set initial node value
		node->data = data;
		node->next = NULL;
	}
	appendNode(sll, node);
}
// Display linked list element
void display(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		return;
	}
	struct LinkNode *temp = sll->head;
	// iterating linked list elements
	while (temp != NULL)
	{
		printf(" %d →", temp->data);
		// Visit to next node
		temp = temp->next;
	}
	printf(" NULL\n");
}
// This are perform the deletion operation 
// by using next upcoming higher node
void deleteNode(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		return;
	}
	// Define some auxiliary variables
	struct LinkNode *current = sll->head;
	struct LinkNode *back = NULL;
	struct LinkNode *auxiliary = NULL;
	// Execute loop until the last node
	while (current != NULL && current->next != NULL)
	{
		if (current->next->data > current->data)
		{
			// When next node value is higher to current node
			// Get current node
			auxiliary = current;
			// visit to next node
			current = current->next;
			if (back == NULL)
			{
				// When need to remove a head node 
				// change head node
				sll->head = current;
			}
			else
			{
				// Connect previous node to next node
				back->next = current;
			}
			// remove current node
			free(auxiliary);
			auxiliary = NULL;
		}
		else
		{
			back = current;
			current = current->next;
		}
	}
}
// Handle request to delete node
void deleteByNextHigher(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		printf("\n Empty Linked List \n");
		return;
	}
	// Display given linked list
	// Before Delete 
	printf("\n Given Linked List  : \n");
	display(sll);
	deleteNode(sll);
	// Display resultant linked list
	// After delete
	printf(" After Delete Linked List  : \n");
	display(sll);
}
int main(int argc, char
	const *argv[])
{
	struct SingleLL *sll1 = newLinkedList();
	struct SingleLL *sll2 = newLinkedList();
	struct SingleLL *sll3 = newLinkedList();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	addNode(sll1, 1);
	addNode(sll1, 2);
	addNode(sll1, 5);
	addNode(sll1, 4);
	addNode(sll1, 3);
	addNode(sll1, 6);
	addNode(sll1, 7);
	addNode(sll1, 8);
	// Second linked list
	//  6 → 3 → 2 → NULL
	addNode(sll2, 6);
	addNode(sll2, 3);
	addNode(sll2, 2);
	// Third linked list
	//  1 → 2 → 3 → NULL
	addNode(sll3, 1);
	addNode(sll3, 2);
	addNode(sll3, 3);
	// Test 
	deleteByNextHigher(sll1);
	deleteByNextHigher(sll2);
	deleteByNextHigher(sll3);
	return 0;
}

Output

 Given Linked List  :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List  :
 5 → 4 → 8 → NULL

 Given Linked List  :
 6 → 3 → 2 → NULL
 After Delete Linked List  :
 6 → 3 → 2 → NULL

 Given Linked List  :
 1 → 2 → 3 → NULL
 After Delete Linked List  :
 3 → NULL
// Include header file
#include <iostream>
using namespace std;
/*
    C++ program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
class LinkNode
{
	public: int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class SingleLL
{
	public: LinkNode *head;
	LinkNode *tail;
	SingleLL()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	// Add new Node at end of linked list 
	void addNode(int data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			// Append the node at last position
			this->tail->next = node;
		}
		this->tail = node;
	}
	// Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			return;
		}
		LinkNode *temp = this->head;
		// iterating linked list elements
		while (temp != NULL)
		{
			cout << " " << temp->data << " →";
			// Visit to next node
			temp = temp->next;
		}
		cout << " NULL\n";
	}
	// This are perform the deletion operation 
	// by using next upcoming higher node
	void deleteNode()
	{
		if (this->head == NULL)
		{
			return;
		}
		// Define some auxiliary variables
		LinkNode *current = this->head;
		LinkNode *back = NULL;
		LinkNode *auxiliary = NULL;
		// Execute loop until the last node
		while (current != NULL && current->next != NULL)
		{
			if (current->next->data > current->data)
			{
				// When next node value is higher to current node
				// Get current node
				auxiliary = current;
				// visit to next node
				current = current->next;
				if (back == NULL)
				{
					// When need to remove a head node 
					// change head node
					this->head = current;
				}
				else
				{
					// Connect previous node to next node
					back->next = current;
				}
				// remove current node
				delete (auxiliary);
				auxiliary = NULL;
			}
			else
			{
				back = current;
				current = current->next;
			}
		}
	}
	// Handle request to delete node
	void deleteByNextHigher()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty Linked List \n";
			return;
		}
		// Display given linked list
		// Before Delete 
		cout << "\n Given Linked List : \n";
		this->display();
		this->deleteNode();
		// Display resultant linked list
		// After delete
		cout << " After Delete Linked List : \n";
		this->display();
	}
};
int main()
{
	SingleLL *sll1 = new SingleLL();
	SingleLL *sll2 = new SingleLL();
	SingleLL *sll3 = new SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	sll1->addNode(1);
	sll1->addNode(2);
	sll1->addNode(5);
	sll1->addNode(4);
	sll1->addNode(3);
	sll1->addNode(6);
	sll1->addNode(7);
	sll1->addNode(8);
	// Second linked list
	//  6 → 3 → 2 → NULL
	sll2->addNode(6);
	sll2->addNode(3);
	sll2->addNode(2);
	// Third linked list
	//  1 → 2 → 3 → NULL
	sll3->addNode(1);
	sll3->addNode(2);
	sll3->addNode(3);
	// Test 
	sll1->deleteByNextHigher();
	sll2->deleteByNextHigher();
	sll3->deleteByNextHigher();
	return 0;
}

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
/*
    Java program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			return;
		}
		LinkNode temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			System.out.print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		System.out.print(" NULL\n");
	}
	// This are perform the deletion operation 
	// by using next upcoming higher node
	public void deleteNode()
	{
		if (this.head == null)
		{
			return;
		}
		// Define some auxiliary variables
		LinkNode current = this.head;
		LinkNode back = null;
		// Execute loop until the last node
		while (current != null && current.next != null)
		{
			if (current.next.data > current.data)
			{
				// When next node value is higher to current node
				// visit to next node
				current = current.next;
				if (back == null)
				{
					// When need to remove a head node 
					// change head node
					this.head = current;
				}
				else
				{
					// Connect previous node to next node
					back.next = current;
				}
			}
			else
			{
				back = current;
				current = current.next;
			}
		}
	}
	// Handle request to delete node
	public void deleteByNextHigher()
	{
		if (this.head == null)
		{
			System.out.print("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Delete 
		System.out.print("\n Given Linked List : \n");
		this.display();
		this.deleteNode();
		// Display resultant linked list
		// After delete
		System.out.print(" After Delete Linked List : \n");
		this.display();
	}
	public static void main(String[] args)
	{
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		SingleLL sll3 = new SingleLL();
		// First linked list
		//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(5);
		sll1.addNode(4);
		sll1.addNode(3);
		sll1.addNode(6);
		sll1.addNode(7);
		sll1.addNode(8);
		// Second linked list
		//  6 → 3 → 2 → NULL
		sll2.addNode(6);
		sll2.addNode(3);
		sll2.addNode(2);
		// Third linked list
		//  1 → 2 → 3 → NULL
		sll3.addNode(1);
		sll3.addNode(2);
		sll3.addNode(3);
		// Test 
		sll1.deleteByNextHigher();
		sll2.deleteByNextHigher();
		sll3.deleteByNextHigher();
	}
}

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
// Include namespace system
using System;
/*
    Csharp program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public LinkNode tail;
	public SingleLL()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			return;
		}
		LinkNode temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			Console.Write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" NULL\n");
	}
	// This are perform the deletion operation 
	// by using next upcoming higher node
	public void deleteNode()
	{
		if (this.head == null)
		{
			return;
		}
		// Define some auxiliary variables
		LinkNode current = this.head;
		LinkNode back = null;
		// Execute loop until the last node
		while (current != null && current.next != null)
		{
			if (current.next.data > current.data)
			{
				// When next node value is higher to current node
				// visit to next node
				current = current.next;
				if (back == null)
				{
					// When need to remove a head node 
					// change head node
					this.head = current;
				}
				else
				{
					// Connect previous node to next node
					back.next = current;
				}
			}
			else
			{
				back = current;
				current = current.next;
			}
		}
	}
	// Handle request to delete node
	public void deleteByNextHigher()
	{
		if (this.head == null)
		{
			Console.Write("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Delete 
		Console.Write("\n Given Linked List : \n");
		this.display();
		this.deleteNode();
		// Display resultant linked list
		// After delete
		Console.Write(" After Delete Linked List : \n");
		this.display();
	}
	public static void Main(String[] args)
	{
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		SingleLL sll3 = new SingleLL();
		// First linked list
		//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(5);
		sll1.addNode(4);
		sll1.addNode(3);
		sll1.addNode(6);
		sll1.addNode(7);
		sll1.addNode(8);
		// Second linked list
		//  6 → 3 → 2 → NULL
		sll2.addNode(6);
		sll2.addNode(3);
		sll2.addNode(2);
		// Third linked list
		//  1 → 2 → 3 → NULL
		sll3.addNode(1);
		sll3.addNode(2);
		sll3.addNode(3);
		// Test 
		sll1.deleteByNextHigher();
		sll2.deleteByNextHigher();
		sll3.deleteByNextHigher();
	}
}

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
package main
import "fmt"
/*
    Go program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
type LinkNode struct {
	data int
	next * LinkNode
}
func getLinkNode(data int) * LinkNode {
	var me *LinkNode = &LinkNode {}
	me.data = data
	me.next = nil
	return me
}
type SingleLL struct {
	head * LinkNode
	tail * LinkNode
}
func getSingleLL() * SingleLL {
	var me *SingleLL = &SingleLL {}
	me.head = nil
	me.tail = nil
	return me
}
// Add new Node at end of linked list 
func(this *SingleLL) addNode(data int) {
	var node * LinkNode = getLinkNode(data)
	if this.head == nil {
		this.head = node
	} else {
		// Append the node at last position
		this.tail.next = node
	}
	this.tail = node
}
// Display linked list element
func(this SingleLL) display() {
	if this.head == nil {
		return
	}
	var temp * LinkNode = this.head
	// iterating linked list elements
	for (temp != nil) {
		fmt.Print(" ", temp.data, " →")
		// Visit to next node
		temp = temp.next
	}
	fmt.Print(" NULL\n")
}
// This are perform the deletion operation 
// by using next upcoming higher node
func(this *SingleLL) deleteNode() {
	if this.head == nil {
		return
	}
	// Define some auxiliary variables
	var current * LinkNode = this.head
	var back * LinkNode = nil
	// Execute loop until the last node
	for (current != nil && current.next != nil) {
		if current.next.data > current.data {
			// When next node value is higher to current node
			// visit to next node
			current = current.next
			if back == nil {
				// When need to remove a head node 
				// change head node
				this.head = current
			} else {
				// Connect previous node to next node
				back.next = current
			}
		} else {
			back = current
			current = current.next
		}
	}
}
// Handle request to delete node
func(this *SingleLL) deleteByNextHigher() {
	if this.head == nil {
		fmt.Print("\n Empty Linked List \n")
		return
	}
	// Display given linked list
	// Before Delete 
	fmt.Print("\n Given Linked List : \n")
	this.display()
	this.deleteNode()
	// Display resultant linked list
	// After delete
	fmt.Print(" After Delete Linked List : \n")
	this.display()
}
func main() {
	var sll1 * SingleLL = getSingleLL()
	var sll2 * SingleLL = getSingleLL()
	var sll3 * SingleLL = getSingleLL()
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	sll1.addNode(1)
	sll1.addNode(2)
	sll1.addNode(5)
	sll1.addNode(4)
	sll1.addNode(3)
	sll1.addNode(6)
	sll1.addNode(7)
	sll1.addNode(8)
	// Second linked list
	//  6 → 3 → 2 → NULL
	sll2.addNode(6)
	sll2.addNode(3)
	sll2.addNode(2)
	// Third linked list
	//  1 → 2 → 3 → NULL
	sll3.addNode(1)
	sll3.addNode(2)
	sll3.addNode(3)
	// Test 
	sll1.deleteByNextHigher()
	sll2.deleteByNextHigher()
	sll3.deleteByNextHigher()
}

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
<?php
/*
    Php program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data)
	{
		$this->data = $data;
		$this->next = NULL;
	}
}
class SingleLL
{
	public $head;
	public $tail;
	public	function __construct()
	{
		$this->head = NULL;
		$this->tail = NULL;
	}
	// Add new Node at end of linked list 
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == NULL)
		{
			$this->head = $node;
		}
		else
		{
			// Append the node at last position
			$this->tail->next = $node;
		}
		$this->tail = $node;
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == NULL)
		{
			return;
		}
		$temp = $this->head;
		// iterating linked list elements
		while ($temp != NULL)
		{
			echo(" ".$temp->data.
				" →");
			// Visit to next node
			$temp = $temp->next;
		}
		echo(" NULL\n");
	}
	// This are perform the deletion operation 
	// by using next upcoming higher node
	public	function deleteNode()
	{
		if ($this->head == NULL)
		{
			return;
		}
		// Define some auxiliary variables
		$current = $this->head;
		$back = NULL;
		// Execute loop until the last node
		while ($current != NULL && $current->next != NULL)
		{
			if ($current->next->data > $current->data)
			{
				// When next node value is higher to current node
				// visit to next node
				$current = $current->next;
				if ($back == NULL)
				{
					// When need to remove a head node 
					// change head node
					$this->head = $current;
				}
				else
				{
					// Connect previous node to next node
					$back->next = $current;
				}
			}
			else
			{
				$back = $current;
				$current = $current->next;
			}
		}
	}
	// Handle request to delete node
	public	function deleteByNextHigher()
	{
		if ($this->head == NULL)
		{
			echo("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Delete 
		echo("\n Given Linked List : \n");
		$this->display();
		$this->deleteNode();
		// Display resultant linked list
		// After delete
		echo(" After Delete Linked List : \n");
		$this->display();
	}
}

function main()
{
	$sll1 = new SingleLL();
	$sll2 = new SingleLL();
	$sll3 = new SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	$sll1->addNode(1);
	$sll1->addNode(2);
	$sll1->addNode(5);
	$sll1->addNode(4);
	$sll1->addNode(3);
	$sll1->addNode(6);
	$sll1->addNode(7);
	$sll1->addNode(8);
	// Second linked list
	//  6 → 3 → 2 → NULL
	$sll2->addNode(6);
	$sll2->addNode(3);
	$sll2->addNode(2);
	// Third linked list
	//  1 → 2 → 3 → NULL
	$sll3->addNode(1);
	$sll3->addNode(2);
	$sll3->addNode(3);
	// Test 
	$sll1->deleteByNextHigher();
	$sll2->deleteByNextHigher();
	$sll3->deleteByNextHigher();
}
main();

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
/*
    Node JS program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			return;
		}
		var temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			process.stdout.write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" NULL\n");
	}
	// This are perform the deletion operation 
	// by using next upcoming higher node
	deleteNode()
	{
		if (this.head == null)
		{
			return;
		}
		// Define some auxiliary variables
		var current = this.head;
		var back = null;
		// Execute loop until the last node
		while (current != null && current.next != null)
		{
			if (current.next.data > current.data)
			{
				// When next node value is higher to current node
				// visit to next node
				current = current.next;
				if (back == null)
				{
					// When need to remove a head node 
					// change head node
					this.head = current;
				}
				else
				{
					// Connect previous node to next node
					back.next = current;
				}
			}
			else
			{
				back = current;
				current = current.next;
			}
		}
	}
	// Handle request to delete node
	deleteByNextHigher()
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Delete 
		process.stdout.write("\n Given Linked List : \n");
		this.display();
		this.deleteNode();
		// Display resultant linked list
		// After delete
		process.stdout.write(" After Delete Linked List : \n");
		this.display();
	}
}

function main()
{
	var sll1 = new SingleLL();
	var sll2 = new SingleLL();
	var sll3 = new SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(5);
	sll1.addNode(4);
	sll1.addNode(3);
	sll1.addNode(6);
	sll1.addNode(7);
	sll1.addNode(8);
	// Second linked list
	//  6 → 3 → 2 → NULL
	sll2.addNode(6);
	sll2.addNode(3);
	sll2.addNode(2);
	// Third linked list
	//  1 → 2 → 3 → NULL
	sll3.addNode(1);
	sll3.addNode(2);
	sll3.addNode(3);
	// Test 
	sll1.deleteByNextHigher();
	sll2.deleteByNextHigher();
	sll3.deleteByNextHigher();
}
main();

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
#    Python 3 program for
#    Delete nodes which have a greater value on right side

#  Linked list node
class LinkNode :
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	def __init__(self) :
		self.head = None
		self.tail = None
	
	#  Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
		else :
			#  Append the node at last position
			self.tail.next = node
		
		self.tail = node
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			return
		
		temp = self.head
		#  iterating linked list elements
		while (temp != None) :
			print("", temp.data ,"→", end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" NULL")
	
	#  This are perform the deletion operation 
	#  by using next upcoming higher node
	def deleteNode(self) :
		if (self.head == None) :
			return
		
		#  Define some auxiliary variables
		current = self.head
		back = None
		#  Execute loop until the last node
		while (current != None and current.next != None) :
			if (current.next.data > current.data) :
				#  When next node value is higher to current node
				#  visit to next node
				current = current.next
				if (back == None) :
					#  When need to remove a head node 
					#  change head node
					self.head = current
				else :
					#  Connect previous node to next node
					back.next = current
				
			else :
				back = current
				current = current.next
			
		
	
	#  Handle request to delete node
	def deleteByNextHigher(self) :
		if (self.head == None) :
			print("\n Empty Linked List ")
			return
		
		#  Display given linked list
		#  Before Delete 
		print("\n Given Linked List : ")
		self.display()
		self.deleteNode()
		#  Display resultant linked list
		#  After delete
		print(" After Delete Linked List : ")
		self.display()
	

def main() :
	sll1 = SingleLL()
	sll2 = SingleLL()
	sll3 = SingleLL()
	#  First linked list
	#   1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	sll1.addNode(1)
	sll1.addNode(2)
	sll1.addNode(5)
	sll1.addNode(4)
	sll1.addNode(3)
	sll1.addNode(6)
	sll1.addNode(7)
	sll1.addNode(8)
	#  Second linked list
	#   6 → 3 → 2 → NULL
	sll2.addNode(6)
	sll2.addNode(3)
	sll2.addNode(2)
	#  Third linked list
	#   1 → 2 → 3 → NULL
	sll3.addNode(1)
	sll3.addNode(2)
	sll3.addNode(3)
	#  Test 
	sll1.deleteByNextHigher()
	sll2.deleteByNextHigher()
	sll3.deleteByNextHigher()

if __name__ == "__main__": main()

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
#    Ruby program for
#    Delete nodes which have a greater value on right side

#  Linked list node
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :data, :next
	attr_accessor :data, :next
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

class SingleLL 
	# Define the accessor and reader of class SingleLL
	attr_reader :head, :tail
	attr_accessor :head, :tail
	def initialize() 
		self.head = nil
		self.tail = nil
	end

	#  Add new Node at end of linked list 
	def addNode(data) 
		node = LinkNode.new(data)
		if (self.head == nil) 
			self.head = node
		else
 
			#  Append the node at last position
			self.tail.next = node
		end

		self.tail = node
	end

	#  Display linked list element
	def display() 
		if (self.head == nil) 
			return
		end

		temp = self.head
		#  iterating linked list elements
		while (temp != nil) 
			print(" ", temp.data ," →")
			#  Visit to next node
			temp = temp.next
		end

		print(" NULL\n")
	end

	#  This are perform the deletion operation 
	#  by using next upcoming higher node
	def deleteNode() 
		if (self.head == nil) 
			return
		end

		#  Define some auxiliary variables
		current = self.head
		back = nil
		#  Execute loop until the last node
		while (current != nil && current.next != nil) 
			if (current.next.data > current.data) 
				#  When next node value is higher to current node
				#  visit to next node
				current = current.next
				if (back == nil) 
					#  When need to remove a head node 
					#  change head node
					self.head = current
				else
 
					#  Connect previous node to next node
					back.next = current
				end

			else
 
				back = current
				current = current.next
			end

		end

	end

	#  Handle request to delete node
	def deleteByNextHigher() 
		if (self.head == nil) 
			print("\n Empty Linked List \n")
			return
		end

		#  Display given linked list
		#  Before Delete 
		print("\n Given Linked List : \n")
		self.display()
		self.deleteNode()
		#  Display resultant linked list
		#  After delete
		print(" After Delete Linked List : \n")
		self.display()
	end

end

def main() 
	sll1 = SingleLL.new()
	sll2 = SingleLL.new()
	sll3 = SingleLL.new()
	#  First linked list
	#   1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	sll1.addNode(1)
	sll1.addNode(2)
	sll1.addNode(5)
	sll1.addNode(4)
	sll1.addNode(3)
	sll1.addNode(6)
	sll1.addNode(7)
	sll1.addNode(8)
	#  Second linked list
	#   6 → 3 → 2 → NULL
	sll2.addNode(6)
	sll2.addNode(3)
	sll2.addNode(2)
	#  Third linked list
	#   1 → 2 → 3 → NULL
	sll3.addNode(1)
	sll3.addNode(2)
	sll3.addNode(3)
	#  Test 
	sll1.deleteByNextHigher()
	sll2.deleteByNextHigher()
	sll3.deleteByNextHigher()
end

main()

Output

 Given Linked List : 
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List : 
 5 → 4 → 8 → NULL

 Given Linked List : 
 6 → 3 → 2 → NULL
 After Delete Linked List : 
 6 → 3 → 2 → NULL

 Given Linked List : 
 1 → 2 → 3 → NULL
 After Delete Linked List : 
 3 → NULL
/*
    Scala program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
class LinkNode(var data: Int,
	var next: LinkNode)
{
	def this(data: Int)
	{
		this(data,null);
	}
}
class SingleLL(var head: LinkNode,
	var tail: LinkNode)
{
	def this()
	{
		this(null,null);
	}
	// Add new Node at end of linked list 
	def addNode(data: Int): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			return;
		}
		var temp: LinkNode = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// This are perform the deletion operation 
	// by using next upcoming higher node
	def deleteNode(): Unit = {
		if (this.head == null)
		{
			return;
		}
		// Define some auxiliary variables
		var current: LinkNode = this.head;
		var back: LinkNode = null;
		// Execute loop until the last node
		while (current != null && current.next != null)
		{
			if (current.next.data > current.data)
			{
				// When next node value is higher to current node
				// visit to next node
				current = current.next;
				if (back == null)
				{
					// When need to remove a head node 
					// change head node
					this.head = current;
				}
				else
				{
					// Connect previous node to next node
					back.next = current;
				}
			}
			else
			{
				back = current;
				current = current.next;
			}
		}
	}
	// Handle request to delete node
	def deleteByNextHigher(): Unit = {
		if (this.head == null)
		{
			print("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Delete 
		print("\n Given Linked List : \n");
		this.display();
		this.deleteNode();
		// Display resultant linked list
		// After delete
		print(" After Delete Linked List : \n");
		this.display();
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var sll1: SingleLL = new SingleLL();
		var sll2: SingleLL = new SingleLL();
		var sll3: SingleLL = new SingleLL();
		// First linked list
		//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(5);
		sll1.addNode(4);
		sll1.addNode(3);
		sll1.addNode(6);
		sll1.addNode(7);
		sll1.addNode(8);
		// Second linked list
		//  6 → 3 → 2 → NULL
		sll2.addNode(6);
		sll2.addNode(3);
		sll2.addNode(2);
		// Third linked list
		//  1 → 2 → 3 → NULL
		sll3.addNode(1);
		sll3.addNode(2);
		sll3.addNode(3);
		// Test 
		sll1.deleteByNextHigher();
		sll2.deleteByNextHigher();
		sll3.deleteByNextHigher();
	}
}

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
/*
    Swift 4 program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	// Add new Node at end of linked list 
	func addNode(_ data: Int)
	{
		let node: LinkNode = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			// Append the node at last position
			self.tail!.next = node;
		}
		self.tail = node;
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			return;
		}
		var temp: LinkNode? = self.head;
		// iterating linked list elements
		while (temp  != nil)
		{
			print("", temp!.data ,"→", terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL");
	}
	// This are perform the deletion operation 
	// by using next upcoming higher node
	func deleteNode()
	{
		if (self.head == nil)
		{
			return;
		}
		// Define some auxiliary variables
		var current: LinkNode? = self.head;
		var back: LinkNode? = nil;
		// Execute loop until the last node
		while (current  != nil && current!.next  != nil)
		{
			if (current!.next!.data > current!.data)
			{
				// When next node value is higher to current node
				// visit to next node
				current = current!.next;
				if (back == nil)
				{
					// When need to remove a head node 
					// change head node
					self.head = current;
				}
				else
				{
					// Connect previous node to next node
					back!.next = current;
				}
			}
			else
			{
				back = current;
				current = current!.next;
			}
		}
	}
	// Handle request to delete node
	func deleteByNextHigher()
	{
		if (self.head == nil)
		{
			print("\n Empty Linked List ");
			return;
		}
		// Display given linked list
		// Before Delete 
		print("\n Given Linked List : ");
		self.display();
		self.deleteNode();
		// Display resultant linked list
		// After delete
		print(" After Delete Linked List : ");
		self.display();
	}
}
func main()
{
	let sll1: SingleLL = SingleLL();
	let sll2: SingleLL = SingleLL();
	let sll3: SingleLL = SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(5);
	sll1.addNode(4);
	sll1.addNode(3);
	sll1.addNode(6);
	sll1.addNode(7);
	sll1.addNode(8);
	// Second linked list
	//  6 → 3 → 2 → NULL
	sll2.addNode(6);
	sll2.addNode(3);
	sll2.addNode(2);
	// Third linked list
	//  1 → 2 → 3 → NULL
	sll3.addNode(1);
	sll3.addNode(2);
	sll3.addNode(3);
	// Test 
	sll1.deleteByNextHigher();
	sll2.deleteByNextHigher();
	sll3.deleteByNextHigher();
}
main();

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL
/*
    Kotlin program for
    Delete nodes which have a greater value on right side
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	// Add new Node at end of linked list 
	fun addNode(data: Int): Unit
	{
		val node: LinkNode = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			// Append the node at last position
			this.tail?.next = node;
		}
		this.tail = node;
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.head == null)
		{
			return;
		}
		var temp: LinkNode ? = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		print(" NULL\n");
	}
	// This are perform the deletion operation 
	// by using next upcoming higher node
	fun deleteNode(): Unit
	{
		if (this.head == null)
		{
			return;
		}
		// Define some auxiliary variables
		var current: LinkNode ? = this.head;
		var back: LinkNode ? = null;
		// Execute loop until the last node
		while (current != null && current.next != null)
		{
			if (current.next!!.data > current.data)
			{
				// When next node value is higher to current node
				// visit to next node
				current = current.next;
				if (back == null)
				{
					// When need to remove a head node 
					// change head node
					this.head = current;
				}
				else
				{
					// Connect previous node to next node
					back.next = current;
				}
			}
			else
			{
				back = current;
				current = current.next;
			}
		}
	}
	// Handle request to delete node
	fun deleteByNextHigher(): Unit
	{
		if (this.head == null)
		{
			print("\n Empty Linked List \n");
			return;
		}
		// Display given linked list
		// Before Delete 
		print("\n Given Linked List : \n");
		this.display();
		this.deleteNode();
		// Display resultant linked list
		// After delete
		print(" After Delete Linked List : \n");
		this.display();
	}
}
fun main(args: Array < String > ): Unit
{
	val sll1: SingleLL = SingleLL();
	val sll2: SingleLL = SingleLL();
	val sll3: SingleLL = SingleLL();
	// First linked list
	//  1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
	sll1.addNode(1);
	sll1.addNode(2);
	sll1.addNode(5);
	sll1.addNode(4);
	sll1.addNode(3);
	sll1.addNode(6);
	sll1.addNode(7);
	sll1.addNode(8);
	// Second linked list
	//  6 → 3 → 2 → NULL
	sll2.addNode(6);
	sll2.addNode(3);
	sll2.addNode(2);
	// Third linked list
	//  1 → 2 → 3 → NULL
	sll3.addNode(1);
	sll3.addNode(2);
	sll3.addNode(3);
	// Test 
	sll1.deleteByNextHigher();
	sll2.deleteByNextHigher();
	sll3.deleteByNextHigher();
}

Output

 Given Linked List :
 1 → 2 → 5 → 4 → 3 → 6 → 7 → 8 → NULL
 After Delete Linked List :
 5 → 4 → 8 → NULL

 Given Linked List :
 6 → 3 → 2 → NULL
 After Delete Linked List :
 6 → 3 → 2 → NULL

 Given Linked List :
 1 → 2 → 3 → NULL
 After Delete Linked List :
 3 → NULL


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved