# Multiply two numbers represented as linked lists into a third list

Here given code implementation process.

``````/*
C program for
Multiply two numbers represented as linked lists into a third list
*/
#include <stdio.h>
#include <stdlib.h>

{
int data;
};
struct SingleLL
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *)
malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
}
return sll;
}
{
// Create dynamic node
if (node == NULL)
{
}
else
{
// Set initial node value
node->data = data;
node->next = next;
}
return node;
}
// Handles the request of adding new node at beginning of linked list
void addNode(struct SingleLL *sll, int data)
{
}
{
if (node == NULL)
{
return;
}
while (temp != NULL)
{
printf(" %d →", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" NULL\n");
}
{
while (temp != NULL)
{
//visit to next node
temp = temp->next;
//change node link to pervious node
//get the reference of newly head node
}
}
struct SingleLL *sll,
int digit)
{
int remainder = 0;
int product = 0;
// Iterate linked list and multiply given digit
while (temp != NULL)
{
if (current == NULL)
{
// Need to create new node
current = newNode(0, NULL);
back->next = current;
}
back = current;
// Calculate product and add previous carry
product += current->data + (temp->data *digit);
// Assign last digit of calculated product
current->data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp->next;
current = current->next;
}
if (product > 0)
{
// Need to add new node
// Create new node
temp = newNode(product, NULL);
// Add node at the last
back->next = temp;
}
}
// Assuming that the linked list contains single digits of number
struct SingleLL *multiply(struct SingleLL *sll1,
struct SingleLL *sll2)
{
{
return result;
}
// First node of resultant list
while (temp != NULL)
{
multiplyByDigit(back, sll1, temp->data);
temp = temp->next;
back = back->next;
}
return result;
}
int main(int argc, char
const *argv[])
{
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
struct SingleLL *sll3 = multiply(sll2, sll1);
return 0;
}``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````/*
Java program for
Multiply two numbers represented as linked lists into a third list
*/
{
public int data;
{
this.data = data;
this.next = next;
}
}
public class SingleLL
{
public SingleLL()
{
}
{
// Create new node
}
public void display()
{
{
return;
}
while (temp != null)
{
System.out.print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
System.out.print(" NULL\n");
}
public void reverseList()
{
while (temp != null)
{
//visit to next node
temp = temp.next;
//change node link to pervious node
//get the reference of newly head node
}
}
public void multiplyByDigit(LinkNode start, int digit)
{
int remainder = 0;
int product = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
if (current == null)
{
// Need to create new node
back.next = current;
}
back = current;
// Calculate product and add previous carry
product += current.data + (temp.data * digit);
// Assign last digit of calculated product
current.data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp.next;
current = current.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back.next = temp;
}
}
// Assuming that the linked list contains single digits of number
public SingleLL multiply(SingleLL other)
{
SingleLL result = new SingleLL();
{
return result;
}
// First node of resultant list
this.reverseList();
other.reverseList();
while (temp != null)
{
this.multiplyByDigit(back, temp.data);
temp = temp.next;
back = back.next;
}
this.reverseList();
other.reverseList();
result.reverseList();
return result;
}
public static void main(String[] args)
{
SingleLL sll1 = new SingleLL();
SingleLL sll2 = new SingleLL();
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
sll1.display();
sll2.display();
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
SingleLL sll3 = sll1.multiply(sll2);
sll3.display();
}
}``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Multiply two numbers represented as linked lists into a third list
*/
{
public:
int data;
{
this->data = data;
this->next = next;
}
};
class SingleLL
{
public:
SingleLL()
{
}
{
// Create new node
}
void display()
{
{
return;
}
while (temp != nullptr)
{
cout << " " << temp->data << " →";
// Visit to next node
temp = temp->next;
}
cout << " NULL\n";
}
void reverseList()
{
while (temp != nullptr)
{
//visit to next node
temp = temp->next;
//change node link to pervious node
//get the reference of newly head node
}
}
{
int remainder = 0;
int product = 0;
// Iterate linked list and multiply given digit
while (temp != nullptr)
{
if (current == nullptr)
{
// Need to create new node
back->next = current;
}
back = current;
// Calculate product and add previous carry
product += current->data + (temp->data *digit);
// Assign last digit of calculated product
current->data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp->next;
current = current->next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back->next = temp;
}
}
// Assuming that the linked list contains single digits of number
SingleLL *multiply(SingleLL *other)
{
SingleLL *result = new SingleLL();
{
return result;
}
// First node of resultant list
this->reverseList();
other->reverseList();
while (temp != nullptr)
{
this->multiplyByDigit(back, temp->data);
temp = temp->next;
back = back->next;
}
this->reverseList();
other->reverseList();
result->reverseList();
return result;
}
};
int main()
{
SingleLL *sll1 = new SingleLL();
SingleLL *sll2 = new SingleLL();
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
cout << " Linked List A \n";
sll1->display();
cout << " Linked List B \n";
sll2->display();
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
SingleLL *sll3 = sll1->multiply(sll2);
cout << " Linked List C \n";
sll3->display();
return 0;
}``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````package main
import "fmt"
/*
Go program for
Multiply two numbers represented as linked lists into a third list
*/
data int
}
}
type SingleLL struct {
}
func getSingleLL() * SingleLL {
return &SingleLL {nil}
}
// Create new node
}
func(this SingleLL) display() {
return
}
for (temp != nil) {
fmt.Print(" ", temp.data, " →")
// Visit to next node
temp = temp.next
}
fmt.Print(" NULL\n")
}
func(this *SingleLL) reverseList() {
var prev * LinkNode = nil
for (temp != nil) {
//visit to next node
temp = temp.next
//change node link to pervious node
//get the reference of newly head node
}
}
func(this *SingleLL) multiplyByDigit(start * LinkNode, digit int) {
var current * LinkNode = start
var back * LinkNode = nil
var product int = 0
// Iterate linked list and multiply given digit
for (temp != nil) {
if current == nil {
// Need to create new node
back.next = current
}
back = current
// Calculate product and add previous carry
product += current.data + (temp.data * digit)
// Assign last digit of calculated product
current.data = product % 10
// Reduce last digit
product = product / 10
// Visit to next node
temp = temp.next
current = current.next
}
if product > 0 {
// Need to add new node
// Create new node
// Add node at the last
back.next = temp
}
}
// Assuming that the linked list contains single digits of number
func(this *SingleLL) multiply(other * SingleLL) * SingleLL {
var result * SingleLL = getSingleLL()
return result
}
// First node of resultant list
this.reverseList()
other.reverseList()
for (temp != nil) {
this.multiplyByDigit(back, temp.data)
temp = temp.next
back = back.next
}
this.reverseList()
other.reverseList()
result.reverseList()
return result
}
func main() {
var sll1 * SingleLL = getSingleLL()
var sll2 * SingleLL = getSingleLL()
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
sll1.display()
sll2.display()
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
var sll3 * SingleLL = sll1.multiply(sll2)
sll3.display()
}``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````// Include namespace system
using System;
/*
Csharp program for
Multiply two numbers represented as linked lists into a third list
*/
{
public int data;
{
this.data = data;
this.next = next;
}
}
public class SingleLL
{
public SingleLL()
{
}
{
// Create new node
}
public void display()
{
{
return;
}
while (temp != null)
{
Console.Write(" " + temp.data.ToString() + " →");
// Visit to next node
temp = temp.next;
}
Console.Write(" NULL\n");
}
public void reverseList()
{
while (temp != null)
{
//visit to next node
temp = temp.next;
//change node link to pervious node
//get the reference of newly head node
}
}
public void multiplyByDigit(LinkNode start, int digit)
{
var current = start;
var product = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
if (current == null)
{
// Need to create new node
back.next = current;
}
back = current;
// Calculate product and add previous carry
product += current.data + (temp.data * digit);
// Assign last digit of calculated product
current.data = product % 10;
// Reduce last digit
product = (int)(product / 10);
// Visit to next node
temp = temp.next;
current = current.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back.next = temp;
}
}
// Assuming that the linked list contains single digits of number
public SingleLL multiply(SingleLL other)
{
var result = new SingleLL();
{
return result;
}
// First node of resultant list
this.reverseList();
other.reverseList();
while (temp != null)
{
this.multiplyByDigit(back, temp.data);
temp = temp.next;
back = back.next;
}
this.reverseList();
other.reverseList();
result.reverseList();
return result;
}
public static void Main(String[] args)
{
var sll1 = new SingleLL();
var sll2 = new SingleLL();
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
sll1.display();
sll2.display();
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
var sll3 = sll1.multiply(sll2);
sll3.display();
}
}``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````<?php
/*
Php program for
Multiply two numbers represented as linked lists into a third list
*/
{
public \$data;
public \$next;
public	function __construct(\$data, \$next)
{
\$this->data = \$data;
\$this->next = \$next;
}
}

class SingleLL
{
public	function __construct()
{
}
{
// Create new node
}
public	function display()
{
{
return;
}
while (\$temp != NULL)
{
printf("%s", " ".strval(\$temp->data).
" →");
// Visit to next node
\$temp = \$temp->next;
}
printf("%s", " NULL\n");
}
public	function reverseList()
{
\$prev = NULL;
while (\$temp != NULL)
{
//visit to next node
\$temp = \$temp->next;
//change node link to pervious node
//get the reference of newly head node
}
}
public	function multiplyByDigit(\$start, \$digit)
{
\$current = \$start;
\$back = NULL;
\$product = 0;
// Iterate linked list and multiply given digit
while (\$temp != NULL)
{
if (\$current == NULL)
{
// Need to create new node
\$back->next = \$current;
}
\$back = \$current;
// Calculate product and add previous carry
\$product += \$current->data + (\$temp->data * \$digit);
// Assign last digit of calculated product
\$current->data = \$product % 10;
// Reduce last digit
\$product = (int)(\$product / 10);
// Visit to next node
\$temp = \$temp->next;
\$current = \$current->next;
}
if (\$product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
\$back->next = \$temp;
}
}
// Assuming that the linked list contains single digits of number
public	function multiply(\$other)
{
\$result = new SingleLL();
{
return \$result;
}
// First node of resultant list
\$this->reverseList();
\$other->reverseList();
while (\$temp != NULL)
{
\$this->multiplyByDigit(\$back, \$temp->data);
\$temp = \$temp->next;
\$back = \$back->next;
}
\$this->reverseList();
\$other->reverseList();
\$result->reverseList();
return \$result;
}
public static
function main(\$args)
{
\$sll1 = new SingleLL();
\$sll2 = new SingleLL();
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
printf("%s", " Linked List A \n");
\$sll1->display();
printf("%s", " Linked List B \n");
\$sll2->display();
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
\$sll3 = \$sll1->multiply(\$sll2);
printf("%s", " Linked List C \n");
\$sll3->display();
}
}

SingleLL::main(array());``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````/*
Node JS program for
Multiply two numbers represented as linked lists into a third list
*/
{
constructor(data, next)
{
this.data = data;
this.next = next;
}
}
class SingleLL
{
constructor()
{
}
{
// Create new node
}
display()
{
{
return;
}
while (temp != null)
{
process.stdout.write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
process.stdout.write(" NULL\n");
}
reverseList()
{
var prev = null;
while (temp != null)
{
//visit to next node
temp = temp.next;
//change node link to pervious node
//get the reference of newly head node
}
}
multiplyByDigit(start, digit)
{
var current = start;
var back = null;
var product = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
if (current == null)
{
// Need to create new node
back.next = current;
}
back = current;
// Calculate product and add previous carry
product += current.data + (temp.data * digit);
// Assign last digit of calculated product
current.data = product % 10;
// Reduce last digit
product = parseInt(product / 10);
// Visit to next node
temp = temp.next;
current = current.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back.next = temp;
}
}
// Assuming that the linked list contains single digits of number
multiply(other)
{
var result = new SingleLL();
{
return result;
}
// First node of resultant list
this.reverseList();
other.reverseList();
while (temp != null)
{
this.multiplyByDigit(back, temp.data);
temp = temp.next;
back = back.next;
}
this.reverseList();
other.reverseList();
result.reverseList();
return result;
}
}

function main()
{
var sll1 = new SingleLL();
var sll2 = new SingleLL();
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
sll1.display();
sll2.display();
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
var sll3 = sll1.multiply(sll2);
sll3.display();
}
// Start program execution
main();``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````#  Python 3 program for
#  Multiply two numbers represented as linked lists into a third list

def __init__(self, data, next) :
self.data = data
self.next = next

class SingleLL :
def __init__(self) :

#  Create new node

def display(self) :
return

while (temp != None) :
print("", temp.data ,"→", end = "")
#  Visit to next node
temp = temp.next

print(" NULL")

# reverse a single linked list
def reverseList(self) :
prev = None
while (temp != None) :
# visit to next node
temp = temp.next
# change node link to pervious node
# get the reference of newly head node

def multiplyByDigit(self, start, digit) :
current = start
back = None
product = 0
#  Iterate linked list and multiply given digit
while (temp != None) :
if (current == None) :
#  Need to create new node
back.next = current

back = current
#  Calculate product and add previous carry
product += current.data + (temp.data * digit)
#  Assign last digit of calculated product
current.data = product % 10
#  Reduce last digit
product = int(product / 10)
#  Visit to next node
temp = temp.next
current = current.next

if (product > 0) :
#  Need to add new node
#  Create new node
#  Add node at the last
back.next = temp

#  Assuming that the linked list contains single digits of number
def multiply(self, other) :
result = SingleLL()
return result

#  First node of resultant list
self.reverseList()
other.reverseList()
while (temp != None) :
self.multiplyByDigit(back, temp.data)
temp = temp.next
back = back.next

self.reverseList()
other.reverseList()
result.reverseList()
return result

def main() :
sll1 = SingleLL()
sll2 = SingleLL()
#  3 → 9 → 7 → 8 → 9 → 8 → NULL
#  9 → 5 → 8 → 8 → 7 → NULL
sll1.display()
sll2.display()
#  397898 *95887 = 38153245526
#  3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
sll3 = sll1.multiply(sll2)
sll3.display()

if __name__ == "__main__": main()``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````#    Ruby program for
#    Multiply two numbers represented as linked lists into a third list

attr_accessor :data, :next
def initialize(data, next)
self.data = data
self.next = next
end

end

class SingleLL
# Define the accessor and reader of class SingleLL
def initialize()
end

#  Create new node
end

def display()
return
end

while (temp != nil)
print(" ", temp.data ," →")
#  Visit to next node
temp = temp.next
end

print(" NULL\n")
end

# reverse a single linked list
def reverseList()
prev = nil
while (temp != nil)
# visit to next node
temp = temp.next
# change node link to pervious node
# get the reference of newly head node
end

end

def multiplyByDigit(start, digit)
current = start
back = nil
product = 0
#  Iterate linked list and multiply given digit
while (temp != nil)
if (current == nil)
#  Need to create new node
back.next = current
end

back = current
#  Calculate product and add previous carry
product += current.data + (temp.data * digit)
#  Assign last digit of calculated product
current.data = product % 10
#  Reduce last digit
product = product / 10
#  Visit to next node
temp = temp.next
current = current.next
end

if (product > 0)
#  Need to add new node
#  Create new node
#  Add node at the last
back.next = temp
end

end

#  Assuming that the linked list contains single digits of number
def multiply(other)
result = SingleLL.new()
return result
end

#  First node of resultant list
self.reverseList()
other.reverseList()
while (temp != nil)
self.multiplyByDigit(back, temp.data)
temp = temp.next
back = back.next
end

self.reverseList()
other.reverseList()
result.reverseList()
return result
end

end

def main()
sll1 = SingleLL.new()
sll2 = SingleLL.new()
#  3 → 9 → 7 → 8 → 9 → 8 → NULL
#  9 → 5 → 8 → 8 → 7 → NULL
sll1.display()
sll2.display()
#  397898 *95887 = 38153245526
#  3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
sll3 = sll1.multiply(sll2)
sll3.display()
end

main()``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````/*
Scala program for
Multiply two numbers represented as linked lists into a third list
*/
{
def this()
{
this(null);
}
def addNode(data: Int): Unit = {
// Create new node
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
def reverseList(): Unit = {
while (temp != null)
{
//visit to next node
temp = temp.next;
//change node link to pervious node
//get the reference of newly head node
}
}
def multiplyByDigit(start: LinkNode, digit: Int): Unit = {
var product: Int = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
if (current == null)
{
// Need to create new node
back.next = current;
}
back = current;
// Calculate product and add previous carry
product += current.data + (temp.data * digit);
// Assign last digit of calculated product
current.data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp.next;
current = current.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back.next = temp;
}
}
// Assuming that the linked list contains single digits of number
def multiply(other: SingleLL): SingleLL = {
var result: SingleLL = new SingleLL();
{
return result;
}
// First node of resultant list
this.reverseList();
other.reverseList();
while (temp != null)
{
this.multiplyByDigit(back, temp.data);
temp = temp.next;
back = back.next;
}
this.reverseList();
other.reverseList();
result.reverseList();
return result;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll1: SingleLL = new SingleLL();
var sll2: SingleLL = new SingleLL();
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
sll1.display();
sll2.display();
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
var sll3: SingleLL = sll1.multiply(sll2);
sll3.display();
}
}``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL``````
``````import Foundation
/*
Java program for
Multiply two numbers represented as linked lists into a third list
*/
{
var data: Int;
init(_ data: Int, _ next: LinkNode? )
{
self.data = data;
self.next = next;
}
}
class SingleLL
{
init()
{
}
{
// Create new node
}
func display()
{
{
return;
}
while (temp  != nil)
{
print(temp!.data , terminator: " → ");
// Visit to next node
temp = temp!.next;
}
print(" NULL\n", terminator: "");
}
func reverseList()
{
while (temp  != nil)
{
//visit to next node
temp = temp!.next;
//change node link to pervious node
//get the reference of newly head node
}
}
func multiplyByDigit(_ start: LinkNode? , _ digit : Int)
{
var product: Int = 0;
// Iterate linked list and multiply given digit
while (temp  != nil)
{
if (current == nil)
{
// Need to create new node
back!.next = current;
}
back = current;
// Calculate product and add previous carry
product += current!.data + (temp!.data * digit);
// Assign last digit of calculated product
current!.data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp!.next;
current = current!.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back!.next = temp;
}
}
// Assuming that the linked list contains single digits of number
func multiply(_ other: SingleLL? )->SingleLL?
{
let result: SingleLL? = SingleLL();
{
return result;
}
// First node of resultant list
self.reverseList();
other!.reverseList();
while (temp  != nil)
{
self.multiplyByDigit(back, temp!.data);
temp = temp!.next;
back = back!.next;
}
self.reverseList();
other!.reverseList();
result!.reverseList();
return result;
}
static func main(_ args: [String])
{
let sll1: SingleLL? = SingleLL();
let sll2: SingleLL? = SingleLL();
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
print(" Linked List A \n", terminator: "");
sll1!.display();
print(" Linked List B \n", terminator: "");
sll2!.display();
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
let sll3: SingleLL? = sll1!.multiply(sll2);
print(" Linked List C \n", terminator: "");
sll3!.display();
}
}
SingleLL.main([String]());``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 →  NULL
9 → 5 → 8 → 8 → 7 →  NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 →  NULL``````
``````/*
Kotlin program for
Multiply two numbers represented as linked lists into a third list
*/
{
var data: Int;
constructor(data: Int, next: LinkNode ? )
{
this.data = data;
this.next = next;
}
}
class SingleLL
{
constructor()
{
}
{
// Create new node
}
fun display(): Unit
{
{
return;
}
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
fun reverseList(): Unit
{
var prev: LinkNode ? = null;
while (temp != null)
{
//visit to next node
temp = temp.next;
//change node link to pervious node
//get the reference of newly head node
}
}
fun multiplyByDigit(start: LinkNode ? , digit : Int): Unit
{
var current: LinkNode ? = start;
var back: LinkNode ? = null;
var product: Int = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
if (current == null)
{
// Need to create new node
back?.next = current;
}
back = current;
// Calculate product and add previous carry
product += current.data + (temp.data * digit);
// Assign last digit of calculated product
current.data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp.next;
current = current.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back?.next = temp;
}
}
// Assuming that the linked list contains single digits of number
fun multiply(other: SingleLL): SingleLL
{
val result: SingleLL = SingleLL();
{
return result;
}
// First node of resultant list
this.reverseList();
other.reverseList();
while (temp != null)
{
this.multiplyByDigit(back, temp.data);
temp = temp.next;
back = back?.next;
}
this.reverseList();
other.reverseList();
result.reverseList();
return result;
}
}
fun main(args: Array < String > ): Unit
{
val sll1: SingleLL = SingleLL();
val sll2: SingleLL = SingleLL();
// 3 → 9 → 7 → 8 → 9 → 8 → NULL
// 9 → 5 → 8 → 8 → 7 → NULL
sll1.display();
sll2.display();
// 397898 *95887 = 38153245526
// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
val sll3: SingleLL = sll1.multiply(sll2);
sll3.display();
}``````

#### Output

`````` Linked List A
3 → 9 → 7 → 8 → 9 → 8 → NULL
9 → 5 → 8 → 8 → 7 → NULL
3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → 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.