Skip to main content

Add two numbers represented by linked lists in kotlin

Kotlin program for Add two numbers represented by linked lists. Here problem description and explanation.

/*
    Kotlin program for
    Add two numbers represented by linked list
*/
// 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;
	}
	fun insert(data: Int): Unit
	{
		val node: LinkNode = LinkNode(data);
		if (this.head == null)
		{
			// Add first node
			this.head = node;
		}
		else
		{
			// Add node at the end position
			this.tail?.next = node;
		}
		// New last 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;
		}
		println(" NULL");
	}
	// Reverse a single linked list
	fun reverse(): Unit
	{
		var prev: LinkNode ? = null;
		var temp: LinkNode ? = this.head;
		while (temp != null)
		{
			// New head node
			this.head = temp;
			// Visit to next node
			temp = temp.next;
			// Change node link to pervious node
			this.head?.next = prev;
			// Get the newly head node
			prev = this.head;
		}
	}
	fun addition(b: SingleLL): SingleLL
	{
		// Reverse both linked lists
		this.reverse();
		b.reverse();
		// Define some variables
		val result: SingleLL = SingleLL();
		var carry: Int = 0;
		var x: LinkNode ? = this.head;
		var y: LinkNode ? = b.head;
		// Iterate the both linked lists
		while (x != null || y != null)
		{
			if (x != null)
			{
				// Add node data of x
				carry += x.data;
				// Visit to next node
				x = x.next;
			}
			if (y != null)
			{
				// Add node data of y
				carry += y.data;
				// Visit to next node
				y = y.next;
			}
			// Add resultant node
			result.insert(carry % 10);
			//  Remove last digit of carry
			carry = carry / 10;
		}
		// Add remaining carry digits
		while (carry != 0)
		{
			// Add resultant node
			result.insert(carry % 10);
			// Remove last digit of carry
			carry = carry / 10;
		}
		// Reverse linked list
		b.reverse();
		this.reverse();
		result.reverse();
		// Return result
		return result;
	}
}
fun main(args: Array < String > ): Unit
{
	val a: SingleLL = SingleLL();
	val b: SingleLL = SingleLL();
	// 5112
	a.insert(5);
	a.insert(1);
	a.insert(1);
	a.insert(2);
	// 99999
	b.insert(9);
	b.insert(9);
	b.insert(9);
	b.insert(9);
	b.insert(9);
	// 5112 + 99999
	// ------------
	// 105111
	val c: SingleLL = a.addition(b);
	c.display();
}

Output

 1 → 0 → 5 → 1 → 1 → 1 → 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