Posted on by Kalkicode
Code Single linked list

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

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

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

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