# Multiply a single digit to number represented as linked list

Here given code implementation process.

``````/*
Java program for
Multiply a single digit to number represented as linked 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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
public void multiplyDigit(int num)
{
if (num > 9 || num < -9 || this.head == null)
{
return;
}
System.out.println(" Multiply By : " + num);
this.reverseList();
// Auxiliary variables
int remainder = 0;
int product = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
// Get current node
back = temp;
// Calculate product and add previous carry
product += (temp.data * num);
// Assign last digit of calculated product
temp.data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back.next = temp;
}
// Back to its original form
this.reverseList();
}
public static void main(String[] args)
{
SingleLL sll = new SingleLL();
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display();
// 723858 X 4
// --------------
// 2895432
sll.multiplyDigit(4);
sll.display();
}
}``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL``````
``````// Include namespace system
using System;
namespace KalkiCode
{
/*
Csharp program for
Multiply a single digit to number represented as linked 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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
public void multiplyDigit(int num)
{
if (num > 9 || num < -9 || this.head == null)
{
return;
}
Console.WriteLine(" Multiply By : " + num.ToString());
this.reverseList();
// Auxiliary variables
var product = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
// Get current node
back = temp;
// Calculate product and add previous carry
product += (temp.data * num);
// Assign last digit of calculated product
temp.data = product % 10;
// Reduce last digit
product = (int)(product / 10);
// Visit to next node
temp = temp.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back.next = temp;
}
// Back to its original form
this.reverseList();
}
public static void Main(String[] args)
{
var sll = new SingleLL();
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display();
// 723858 X 4
// --------------
// 2895432
sll.multiplyDigit(4);
sll.display();
}
}
}``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Multiply a single digit to number represented as linked list
*/
{
public:
int data;
{
this->data = data;
this->next = next;
}
};
class SingleLL
{
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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
void multiplyDigit(int num)
{
if (num > 9 || num < -9 || this->head == nullptr)
{
return;
}
cout << " Multiply By : " << num << endl;
this->reverseList();
// Auxiliary variables
int remainder = 0;
int product = 0;
// Iterate linked list and multiply given digit
while (temp != nullptr)
{
// Get current node
back = temp;
// Calculate product and add previous carry
product += (temp->data *num);
// Assign last digit of calculated product
temp->data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp->next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back->next = temp;
}
// Back to its original form
this->reverseList();
}
};
int main()
{
SingleLL *sll = new SingleLL();
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
cout << " Linked List \n";
sll->display();
// 723858 X 4
// --------------
// 2895432
sll->multiplyDigit(4);
cout << " Linked List \n";
sll->display();
return 0;
}``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL``````
``````/*
C program for
Multiply a single digit to number represented as linked 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
}

}
// Assuming that the linked list contains single digits of number
// num is single digit
void multiplyDigit(struct SingleLL *sll, int num)
{
if(num > 9 || num < -9 || sll->head == NULL)
{
return;
}

printf(" Multiply By : %d \n",num);

// Auxiliary variables

int remainder = 0;
int product = 0;

// Iterate linked list and multiply given digit
while(temp != NULL)
{
// Get current node
back = temp;

// Calculate product and add previous carry
product += (temp->data * num );

// Assign last digit of calculated product
temp->data = product % 10;

// Reduce last digit
product = product / 10;

// Visit to next node
temp = temp->next;
}

if(product > 0)
{
// Need to add new node
// Create new node
temp =  newNode(product,NULL);

// Add node at the last
back->next  = temp;
}

// Back to its original form

}

int main(int argc, char const *argv[])
{
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
// 723858 X 4
// --------------
// 2895432
multiplyDigit(sll,4);
return 0;
}``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL``````
``````package main
import "fmt"
/*
Go program for
Multiply a single digit to number represented as linked list
*/
data int
}
me.data = data
me.next = next
return me
}
type SingleLL struct {
}
func getSingleLL() * SingleLL {
var me *SingleLL = &SingleLL {}
return me
}
// 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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
func(this *SingleLL) multiplyDigit(num int) {
if num > 9 || num < -9 || this.head == nil {
return
}
fmt.Println(" Multiply By : ", num)
this.reverseList()
// Auxiliary variables
var back * LinkNode = nil
var product int = 0
// Iterate linked list and multiply given digit
for (temp != nil) {
// Get current node
back = temp
// Calculate product and add previous carry
product += (temp.data * num)
// Assign last digit of calculated product
temp.data = product % 10
// Reduce last digit
product = product / 10
// Visit to next node
temp = temp.next
}
if product > 0 {
// Need to add new node
// Create new node
// Add node at the last
back.next = temp
}
// Back to its original form
this.reverseList()
}
func main() {
var sll * SingleLL = getSingleLL()
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display()
// 723858 X 4
// --------------
// 2895432
sll.multiplyDigit(4)
sll.display()
}``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL``````
``````<?php
/*
Php program for
Multiply a single digit to number represented as linked 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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
public	function multiplyDigit(\$num)
{
if (\$num > 9 || \$num < -9 || \$this->head == NULL)
{
return;
}
printf("%s\n", " Multiply By : ".strval(\$num));
\$this->reverseList();
// Auxiliary variables
\$back = NULL;
\$product = 0;
// Iterate linked list and multiply given digit
while (\$temp != NULL)
{
// Get current node
\$back = \$temp;
// Calculate product and add previous carry
\$product += (\$temp->data * \$num);
// Assign last digit of calculated product
\$temp->data = \$product % 10;
// Reduce last digit
\$product = (int)(\$product / 10);
// Visit to next node
\$temp = \$temp->next;
}
if (\$product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
\$back->next = \$temp;
}
// Back to its original form
\$this->reverseList();
}
public static
function main(\$args)
{
\$sll = new SingleLL();
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
\$sll->display();
// 723858 X 4
// --------------
// 2895432
\$sll->multiplyDigit(4);
\$sll->display();
}
}

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

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL``````
``````/*
Node JS program for
Multiply a single digit to number represented as linked 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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
multiplyDigit(num)
{
if (num > 9 || num < -9 || this.head == null)
{
return;
}
console.log(" Multiply By : " + num);
this.reverseList();
// Auxiliary variables
var back = null;
var product = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
// Get current node
back = temp;
// Calculate product and add previous carry
product += (temp.data * num);
// Assign last digit of calculated product
temp.data = product % 10;
// Reduce last digit
product = parseInt(product / 10);
// Visit to next node
temp = temp.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back.next = temp;
}
// Back to its original form
this.reverseList();
}
}

function main()
{
var sll = new SingleLL();
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display();
// 723858 X 4
// --------------
// 2895432
sll.multiplyDigit(4);
sll.display();
}
// Start program execution
main();``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL``````
``````#    Python 3 program for
#    Multiply a single digit to number represented as linked 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

#  Assuming that the linked list contains single digits of number
#  num is single digit
def multiplyDigit(self, num) :
if (num > 9 or num < -9 or self.head == None) :
return

print(" Multiply By : ", num)
self.reverseList()
#  Auxiliary variables
back = None
product = 0
#  Iterate linked list and multiply given digit
while (temp != None) :
#  Get current node
back = temp
#  Calculate product and add previous carry
product += (temp.data * num)
#  Assign last digit of calculated product
temp.data = product % 10
#  Reduce last digit
product = int(product / 10)
#  Visit to next node
temp = temp.next

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

#  Back to its original form
self.reverseList()

def main() :
sll = SingleLL()
#  7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display()
#  723858 X 4
#  --------------
#  2895432
sll.multiplyDigit(4)
sll.display()

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

#### Output

`````` Linked List
7  →  2  →  3  →  8  →  5  →  8  → NULL
Multiply By :  4
2  →  8  →  9  →  5  →  4  →  3  →  2  → NULL``````
``````#    Ruby program for
#    Multiply a single digit to number represented as linked list

attr_accessor :data, :next
def initialize(data, nexts)
self.data = data
self.next = nexts
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

#  Assuming that the linked list contains single digits of number
#  num is single digit
def multiplyDigit(num)
if (num > 9 || num < -9 || self.head == nil)
return
end

print(" Multiply By : ", num, "\n")
self.reverseList()
#  Auxiliary variables
back = nil
product = 0
#  Iterate linked list and multiply given digit
while (temp != nil)
#  Get current node
back = temp
#  Calculate product and add previous carry
product += (temp.data * num)
#  Assign last digit of calculated product
temp.data = product % 10
#  Reduce last digit
product = product / 10
#  Visit to next node
temp = temp.next
end

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

#  Back to its original form
self.reverseList()
end

end

def main()
sll = SingleLL.new()
#  7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display()
#  723858 X 4
#  --------------
#  2895432
sll.multiplyDigit(4)
sll.display()
end

main()``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL
``````
``````/*
Scala program for
Multiply a single digit to number represented as linked 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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
def multiplyDigit(num: Int): Unit = {
if (num > 9 || num < -9 || this.head == null)
{
return;
}
println(" Multiply By : " + num);
this.reverseList();
// Auxiliary variables
var product: Int = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
// Get current node
back = temp;
// Calculate product and add previous carry
product += (temp.data * num);
// Assign last digit of calculated product
temp.data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back.next = temp;
}
// Back to its original form
this.reverseList();
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll: SingleLL = new SingleLL();
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display();
// 723858 X 4
// --------------
// 2895432
sll.multiplyDigit(4);
sll.display();
}
}``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL``````
``````/*
Swift 4 program for
Multiply a single digit to number represented as linked 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");
}
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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
func multiplyDigit(_ num: Int)
{
if (num > 9 ||
num < -9 ||
{
return;
}
print(" Multiply By : ", num);
self.reverseList();
// Auxiliary variables
var product: Int = 0;
// Iterate linked list and multiply given digit
while (temp  != nil)
{
// Get current node
back = temp;
// Calculate product and add previous carry
product += (temp!.data * num);
// Assign last digit of calculated product
temp!.data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp!.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back!.next = temp;
}
// Back to its original form
self.reverseList();
}
}
func main()
{
let sll: SingleLL = SingleLL();
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display();
// 723858 X 4
// --------------
// 2895432
sll.multiplyDigit(4);
sll.display();
}
main();``````

#### Output

`````` Linked List
7 →  2 →  3 →  8 →  5 →  8 → NULL
Multiply By :  4
2 →  8 →  9 →  5 →  4 →  3 →  2 → NULL``````
``````/*
Kotlin program for
Multiply a single digit to number represented as linked 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
}
}
// Assuming that the linked list contains single digits of number
// num is single digit
fun multiplyDigit(num: Int): Unit
{
if (num > 9 || num < -9 || this.head == null)
{
return;
}
println(" Multiply By : " + num);
this.reverseList();
// Auxiliary variables
var back: LinkNode ? = null;
var product: Int = 0;
// Iterate linked list and multiply given digit
while (temp != null)
{
// Get current node
back = temp;
// Calculate product and add previous carry
product += (temp.data * num);
// Assign last digit of calculated product
temp.data = product % 10;
// Reduce last digit
product = product / 10;
// Visit to next node
temp = temp.next;
}
if (product > 0)
{
// Need to add new node
// Create new node
// Add node at the last
back?.next = temp;
}
// Back to its original form
this.reverseList();
}
}
fun main(args: Array < String > ): Unit
{
val sll: SingleLL = SingleLL();
// 7 → 2 → 3 → 8 → 5 → 8 → NULL
sll.display();
// 723858 X 4
// --------------
// 2895432
sll.multiplyDigit(4);
sll.display();
}``````

#### Output

`````` Linked List
7 → 2 → 3 → 8 → 5 → 8 → NULL
Multiply By : 4
2 → 8 → 9 → 5 → 4 → 3 → 2 → 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.