Skip to main content

Delete all odd nodes of a circular linked list in kotlin

Kotlin program for Delete all odd nodes of a circular linked list. Here more information.

// Kotlin program for
// Delete all odd nodes of a circular linked list
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
}
class CircularLinkedList
{
	var head: LinkNode ? ;
	var tail: LinkNode ? ;
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	fun insert(value: Int): Unit
	{
		val node: LinkNode = LinkNode(value);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			this.tail?.next = node;
		}
		node.next = this.head;
		this.tail = node;
	}
	// Display node element of circular linked list
	fun display(): Unit
	{
		if (this.head == null)
		{
			println("\n Empty Linked List");
		}
		else
		{
			// First node of linked list
			print("  " + this.head!!.data);
			var temp: LinkNode ? = this.head?.next;
			// Display linked list node
			while (temp != null && temp != this.head)
			{
				// Display node value
				print("  " + temp.data);
				// Visit to next node
				temp = temp.next;
			}
		}
	}
	fun removeOddNode(): Unit
	{
		if (this.head == null)
		{
			return;
		}
		var temp: LinkNode ? = this.head;
		var auxiliary: LinkNode ? ;
		var point: LinkNode ? = null;
		while (temp != null)
		{
			// Check if node is odd or not
			if ((temp.data and 1) != 0)
			{
				auxiliary = temp;
				// Vist to next node
				temp = temp.next;
				if (auxiliary == this.head)
				{
					// Remove head node
					if (auxiliary == this.tail)
					{
						// Removing a single node
						this.tail = null;
						this.head = null;
						temp = null;
						point = null;
					}
					else
					{
						this.head = temp;
						this.tail?.next = temp;
					}
				}
				else if (auxiliary == this.tail)
				{
					// When remove last node
					if (point != null)
					{
						point.next = this.head;
					}
					this.tail = point;
				}
				else
				{
					// When removing intermediate node
					if (point != null)
					{
						point.next = temp;
					}
				}
			}
			else
			{
				point = temp;
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// Stop the process
					temp = null;
				}
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val cll: CircularLinkedList = CircularLinkedList();
	// Insert node
	cll.insert(3);
	cll.insert(12);
	cll.insert(32);
	cll.insert(1);
	cll.insert(14);
	cll.insert(27);
	cll.insert(0);
	cll.insert(13);
	cll.insert(89);
	println("\n Before Remove Odd Nodes");
	cll.display();
	/*
	    Node  Odd
	    ----- ------------
	     3       Yes
	    12       No
	    32       No
	    1        Yes
	    14       No
	    27       Yes 
	     0        No
	    13       Yes
	    89       Yes
	    -------------------
	    Result 
	    ------
	    12 → 32 → 14 → 0 ┐ 
	     └--------------⤶
	*/
	cll.removeOddNode();
	println("\n After Remove Odd Nodes");
	cll.display();
}

Output

 Before Remove Odd Nodes
  3  12  32  1  14  27  0  13  89
 After Remove Odd Nodes
  12  32  14  0




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