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

``````/*
Kotlin program for
*/
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
fun insert(data: Int): Unit
{
{
}
else
{
// Add node at the end position
this.tail?.next = node;
}
// New last node
this.tail = node;
}
fun display(): Unit
{
{
return;
}
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;
while (temp != null)
{
// Visit to next node
temp = temp.next;
// Change node link to pervious node
// Get the newly head node
}
}
{
this.reverse();
b.reverse();
// Define some variables
val result: SingleLL = SingleLL();
var carry: Int = 0;
// 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;
}
result.insert(carry % 10);
//  Remove last digit of carry
carry = carry / 10;
}
while (carry != 0)
{
result.insert(carry % 10);
// Remove last digit of carry
carry = carry / 10;
}
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
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.