Skip to main content

Delete all node which is less than from right node of linked list in kotlin

Kotlin program for Delete all node which is less than from right node of linked list. Here problem description and explanation.

/*
    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 remove all node 
	// which is less than next node
	fun deleteByNextHigher(): Unit
	{
		if (this.head == null)
		{
			println("\n Empty Linked List");
			return;
		}
		// Display given linked list
		// Before Delete 
		println("\n Given Linked List : ");
		this.display();
		this.deleteNode();
		// Display resultant linked list
		// After delete
		println(" After Delete Linked List : ");
		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




Comment

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