Skip to main content

Convert singly linked list to circular list in typescript

Ts program for Convert singly linked list to circular list. Here problem description and explanation.

// TypeScript program for
// Convert singly linked list into circular list

// Define class of linked list Node
class LinkNode
{
	public data: number;
	public next: LinkNode;
	constructor(data: number)
	{
		this.data = data;
		this.next = null;
	}
}
class LinkedList
{
	public head: LinkNode;
	// Class constructor
	constructor()
	{
		this.head = null;
	}
	// Check circular linked list or not
	// Note that this function is not capable to detect loop
	public boolean isCircular()
	{
		if (this.head == null)
		{
			// Case when linked list is empty
			return false;
		}
		else
		{
			var temp = this.head;
			while (temp != null)
			{
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// When detecting circular node
					return true;
				}
			}
			// When not circular linked list
			return false;
		}
	}
	// Display node element of linked list
	public display()
	{
		if (this.head == null)
		{
			console.log("Empty Linked List");
		}
		else
		{
			console.log("Linked List Element :");
			var temp = this.head;
			// Iterate linked list
			while (temp != null)
			{
				// Display node
				console.log("  " + temp.data);
				// Visit to next node
				temp = temp.next;
				if (temp == this.head)
				{
					// Stop iteration
					return;
				}
			}
			console.log();
		}
	}
	// Coverted circular Linked list
	public makeCircular()
	{
		if (this.head == null)
		{
			console.log("Empty Linked List");
		}
		else
		{
			var temp = this.head;
			// Find last node
			while (temp.next != null)
			{
				temp = temp.next;
				if (temp == this.head)
				{
					// Already circular Linked list
					return;
				}
			}
			// Connect last node to first node
			temp.next = this.head;
		}
	}
	public static main(args: string[])
	{
		var ll = new LinkedList();
		// Insert element of linked list
		ll.head = new LinkNode(1);
		ll.head.next = new LinkNode(2);
		ll.head.next.next = new LinkNode(3);
		ll.head.next.next.next = new LinkNode(4);
		ll.head.next.next.next.next = new LinkNode(5);
		ll.head.next.next.next.next.next = new LinkNode(6);
		ll.head.next.next.next.next.next.next = new LinkNode(7);
		ll.display();
		if (ll.isCircular())
		{
			console.log("Circular Yes");
		}
		else
		{
			console.log("Circular No");
		}
		console.log("After Convert");
		ll.makeCircular();
		if (ll.isCircular())
		{
			console.log("Circular Yes");
		}
		else
		{
			console.log("Circular No");
		}
	}
}
LinkedList.main([]);
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */

Output

Linked List Element :
  1
  2
  3
  4
  5
  6
  7

Circular No
After Convert
Circular Yes




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