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
*/
// Linked list node
class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data, LinkNode next)
	{
		this.data = data;
		this.next = next;
	}
}
public class SingleLL
{
	public LinkNode head;
	public SingleLL()
	{
		this.head = null;
	}
	// Adding new node at beginning of linked list
	public void addNode(int data)
	{
		// Create new node
		this.head = new LinkNode(data, this.head);
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			return;
		}
		LinkNode temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			System.out.print(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		System.out.print(" NULL\n");
	}
	//reverse a single linked list
	public void reverseList()
	{
		LinkNode prev = null;
		LinkNode temp = 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 reference of newly head node
			prev = this.head;
		}
	}
	// 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
		LinkNode temp = this.head;
		LinkNode back = null;
		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 = new LinkNode(product, null);
			// 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();
		// Linked list
		// 7 → 2 → 3 → 8 → 5 → 8 → NULL
		sll.addNode(8);
		sll.addNode(5);
		sll.addNode(8);
		sll.addNode(3);
		sll.addNode(2);
		sll.addNode(7);
		System.out.print(" Linked List \n");
		sll.display();
		// 723858 X 4
		// --------------
		// 2895432   
		sll.multiplyDigit(4);
		System.out.print(" Linked List \n");
		sll.display();
	}
}

Output

 Linked List
 7 → 2 → 3 → 8 → 5 → 8 → NULL
 Multiply By : 4
 Linked List
 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
	*/
	// Linked list node
	public class LinkNode
	{
		public int data;
		public LinkNode next;
		public LinkNode(int data, LinkNode next)
		{
			this.data = data;
			this.next = next;
		}
	}
	public class SingleLL
	{
		public LinkNode head;
		public SingleLL()
		{
			this.head = null;
		}
		// Adding new node at beginning of linked list
		public void addNode(int data)
		{
			// Create new node
			this.head = new LinkNode(data, this.head);
		}
		// Display linked list element
		public void display()
		{
			if (this.head == null)
			{
				return;
			}
			var temp = this.head;
			// iterating linked list elements
			while (temp != null)
			{
				Console.Write(" " + temp.data.ToString() + " →");
				// Visit to next node
				temp = temp.next;
			}
			Console.Write(" NULL\n");
		}
		//reverse a single linked list
		public void reverseList()
		{
			LinkNode prev = null;
			var temp = 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 reference of newly head node
				prev = this.head;
			}
		}
		// 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 temp = this.head;
			LinkNode 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 = (int)(product / 10);
				// Visit to next node
				temp = temp.next;
			}
			if (product > 0)
			{
				// Need to add new node
				// Create new node
				temp = new LinkNode(product, null);
				// 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();
			// Linked list
			// 7 → 2 → 3 → 8 → 5 → 8 → NULL
			sll.addNode(8);
			sll.addNode(5);
			sll.addNode(8);
			sll.addNode(3);
			sll.addNode(2);
			sll.addNode(7);
			Console.Write(" Linked List \n");
			sll.display();
			// 723858 X 4
			// --------------
			// 2895432   
			sll.multiplyDigit(4);
			Console.Write(" Linked List \n");
			sll.display();
		}
	}
}

Output

 Linked List
 7 → 2 → 3 → 8 → 5 → 8 → NULL
 Multiply By : 4
 Linked List
 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
*/
// Linked list node
class LinkNode
{
	public: 
    int data;
	LinkNode *next;
	LinkNode(int data, LinkNode *next)
	{
		this->data = data;
		this->next = next;
	}
};
class SingleLL
{
	public: LinkNode *head;
	SingleLL()
	{
		this->head = nullptr;
	}
	// Adding new node at beginning of linked list
	void addNode(int data)
	{
		// Create new node
		this->head = new LinkNode(data, this->head);
	}
	// Display linked list element
	void display()
	{
		if (this->head == nullptr)
		{
			return;
		}
		LinkNode *temp = this->head;
		// iterating linked list elements
		while (temp != nullptr)
		{
			cout << " " << temp->data << " →";
			// Visit to next node
			temp = temp->next;
		}
		cout << " NULL\n";
	}
	//reverse a single linked list
	void reverseList()
	{
		LinkNode *prev = nullptr;
		LinkNode *temp = this->head;
		while (temp != nullptr)
		{
			//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 reference of newly head node
			prev = this->head;
		}
	}
	// 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
		LinkNode *temp = this->head;
		LinkNode *back = nullptr;
		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
			temp = new LinkNode(product, nullptr);
			// Add node at the last
			back->next = temp;
		}
		// Back to its original form
		this->reverseList();
	}
};
int main()
{
	SingleLL *sll = new SingleLL();
	// Linked list
	// 7 → 2 → 3 → 8 → 5 → 8 → NULL
	sll->addNode(8);
	sll->addNode(5);
	sll->addNode(8);
	sll->addNode(3);
	sll->addNode(2);
	sll->addNode(7);
	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
 Linked List
 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>

// Linked List LinkNode
struct LinkNode
{
    int data;
    struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
    struct LinkNode *head;
};

// Returns the new linked list
struct SingleLL *newLinkedList()
{
    // Create memory of head and tail Nodes
    struct SingleLL *sll = (struct SingleLL *)
    malloc(sizeof(struct SingleLL));
    if (sll == NULL)
    {
        printf("Memory overflow\n");
    }
    else
    {
        sll->head = NULL;
    }
    return sll;
}

struct LinkNode *newNode(int data,struct LinkNode *next)
{
     // Create dynamic node
    struct LinkNode *node = (struct LinkNode *)
    malloc(sizeof(struct LinkNode));
    if (node == NULL)
    {
        printf("Memory overflow to Create LinkNode\n");
    }
    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)
{
  
    sll->head  = newNode(data,sll->head);

}


// Display linked list element
void display(struct LinkNode *node)
{
    if (node == NULL)
    {
        return;
    }
    struct LinkNode *temp = node;
    // iterating linked list elements
    while (temp != NULL)
    {
        printf(" %d →", temp->data);
        // Visit to next node
        temp = temp->next;
    }
    printf(" NULL\n");
}

//reverse a single linked list
struct LinkNode* reverseList(struct LinkNode*head){
 
    struct LinkNode*prev=NULL;

    struct LinkNode*temp= head;

    while(temp!=NULL)
    {
      //new head node
      head = temp;
      
      //visit to next node
      temp=temp->next;
      //change node link to pervious node
      head->next=prev;
      //get the reference of newly head node
      prev=head;
    }

    return head;

}
// 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);
    sll->head = reverseList(sll->head);

    // Auxiliary variables
    struct LinkNode*temp = sll->head;
    struct LinkNode*back = NULL;

    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
    sll->head = reverseList(sll->head);

}

int main(int argc, char const *argv[])
{
    struct SingleLL *sll = newLinkedList();
    // Linked list
    // 7 → 2 → 3 → 8 → 5 → 8 → NULL
    addNode(sll, 8);
    addNode(sll, 5);
    addNode(sll, 8);
    addNode(sll, 3);
    addNode(sll, 2);
    addNode(sll, 7);
    printf(" Linked List \n");
    display(sll->head);
    // 723858 X 4
    // --------------
    // 2895432   
    multiplyDigit(sll,4);
    printf(" Linked List \n");
    display(sll->head);
    return 0;
}

Output

 Linked List
 7 → 2 → 3 → 8 → 5 → 8 → NULL
 Multiply By : 4
 Linked List
 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
*/
// Linked list node
type LinkNode struct {
	data int
	next * LinkNode
}
func getLinkNode(data int, next * LinkNode) * LinkNode {
	var me *LinkNode = &LinkNode {}
	me.data = data
	me.next = next
	return me
}
type SingleLL struct {
	head * LinkNode
}
func getSingleLL() * SingleLL {
	var me *SingleLL = &SingleLL {}
	me.head = nil
	return me
}
// Adding new node at beginning of linked list
func(this *SingleLL) addNode(data int) {
	// Create new node
	this.head = getLinkNode(data, this.head)
}
// Display linked list element
func(this SingleLL) display() {
	if this.head == nil {
		return
	}
	var temp * LinkNode = this.head
	// iterating linked list elements
	for (temp != nil) {
		fmt.Print(" ", temp.data, " →")
		// Visit to next node
		temp = temp.next
	}
	fmt.Print(" NULL\n")
}
//reverse a single linked list
func(this *SingleLL) reverseList() {
	var prev * LinkNode = nil
	var temp * LinkNode = this.head
	for (temp != nil) {
		//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 reference of newly head node
		prev = this.head
	}
}
// 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 temp * LinkNode = this.head
	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
		temp = getLinkNode(product, nil)
		// Add node at the last
		back.next = temp
	}
	// Back to its original form
	this.reverseList()
}
func main() {
	var sll * SingleLL = getSingleLL()
	// Linked list
	// 7 → 2 → 3 → 8 → 5 → 8 → NULL
	sll.addNode(8)
	sll.addNode(5)
	sll.addNode(8)
	sll.addNode(3)
	sll.addNode(2)
	sll.addNode(7)
	fmt.Print(" Linked List \n")
	sll.display()
	// 723858 X 4
	// --------------
	// 2895432   
	sll.multiplyDigit(4)
	fmt.Print(" Linked List \n")
	sll.display()
}

Output

 Linked List
 7 → 2 → 3 → 8 → 5 → 8 → NULL
 Multiply By : 4
 Linked List
 2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL
<?php
/*
    Php program for
    Multiply a single digit to number represented as linked list
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;
	public	function __construct($data, $next)
	{
		$this->data = $data;
		$this->next = $next;
	}
}

class SingleLL
{
	public $head;
	public	function __construct()
	{
		$this->head = NULL;
	}
	// Adding new node at beginning of linked list
	public	function addNode($data)
	{
		// Create new node
		$this->head = new LinkNode($data, $this->head);
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == NULL)
		{
			return;
		}
		$temp = $this->head;
		// iterating linked list elements
		while ($temp != NULL)
		{
			printf("%s", " ".strval($temp->data).
				" →");
			// Visit to next node
			$temp = $temp->next;
		}
		printf("%s", " NULL\n");
	}
	//reverse a single linked list
	public	function reverseList()
	{
		$prev = NULL;
		$temp = $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 reference of newly head node
			$prev = $this->head;
		}
	}
	// 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
		$temp = $this->head;
		$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
			$temp = new LinkNode($product, NULL);
			// Add node at the last
			$back->next = $temp;
		}
		// Back to its original form
		$this->reverseList();
	}
	public static
	function main($args)
	{
		$sll = new SingleLL();
		// Linked list
		// 7 → 2 → 3 → 8 → 5 → 8 → NULL
		$sll->addNode(8);
		$sll->addNode(5);
		$sll->addNode(8);
		$sll->addNode(3);
		$sll->addNode(2);
		$sll->addNode(7);
		printf("%s", " Linked List \n");
		$sll->display();
		// 723858 X 4
		// --------------
		// 2895432   
		$sll->multiplyDigit(4);
		printf("%s", " Linked List \n");
		$sll->display();
	}
}

SingleLL::main(array());

Output

 Linked List
 7 → 2 → 3 → 8 → 5 → 8 → NULL
 Multiply By : 4
 Linked List
 2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL
/*
    Node JS program for
    Multiply a single digit to number represented as linked list
*/
// Linked list node
class LinkNode
{
	constructor(data, next)
	{
		this.data = data;
		this.next = next;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
	}
	// Adding new node at beginning of linked list
	addNode(data)
	{
		// Create new node
		this.head = new LinkNode(data, this.head);
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			return;
		}
		var temp = this.head;
		// iterating linked list elements
		while (temp != null)
		{
			process.stdout.write(" " + temp.data + " →");
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" NULL\n");
	}
	//reverse a single linked list
	reverseList()
	{
		var prev = null;
		var temp = 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 reference of newly head node
			prev = this.head;
		}
	}
	// 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 temp = this.head;
		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
			temp = new LinkNode(product, null);
			// Add node at the last
			back.next = temp;
		}
		// Back to its original form
		this.reverseList();
	}
}

function main()
{
	var sll = new SingleLL();
	// Linked list
	// 7 → 2 → 3 → 8 → 5 → 8 → NULL
	sll.addNode(8);
	sll.addNode(5);
	sll.addNode(8);
	sll.addNode(3);
	sll.addNode(2);
	sll.addNode(7);
	process.stdout.write(" Linked List \n");
	sll.display();
	// 723858 X 4
	// --------------
	// 2895432   
	sll.multiplyDigit(4);
	process.stdout.write(" Linked List \n");
	sll.display();
}
// Start program execution
main();

Output

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

#  Linked list node
class LinkNode :
	def __init__(self, data, next) :
		self.data = data
		self.next = next
	

class SingleLL :
	def __init__(self) :
		self.head = None
	
	#  Adding new node at beginning of linked list
	def addNode(self, data) :
		#  Create new node
		self.head = LinkNode(data, self.head)
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			return
		
		temp = self.head
		#  iterating linked list elements
		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
		temp = self.head
		while (temp != None) :
			# new head node
			self.head = temp
			# visit to next node
			temp = temp.next
			# change node link to pervious node
			self.head.next = prev
			# get the reference of newly head node
			prev = self.head
		
	
	#  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
		temp = self.head
		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
			temp = LinkNode(product, None)
			#  Add node at the last
			back.next = temp
		
		#  Back to its original form
		self.reverseList()
	

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

if __name__ == "__main__": main()

Output

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

#  Linked list node
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :data, :next
	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
	attr_reader :head
	attr_accessor :head
	def initialize() 
		self.head = nil
	end

	#  Adding new node at beginning of linked list
	def addNode(data) 
		#  Create new node
		self.head = LinkNode.new(data, self.head)
	end

	#  Display linked list element
	def display() 
		if (self.head == nil) 
			return
		end

		temp = self.head
		#  iterating linked list elements
		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
		temp = self.head
		while (temp != nil) 
			# new head node
			self.head = temp
			# visit to next node
			temp = temp.next
			# change node link to pervious node
			self.head.next = prev
			# get the reference of newly head node
			prev = self.head
		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
		temp = self.head
		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
			temp = LinkNode.new(product, nil)
			#  Add node at the last
			back.next = temp
		end

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

end

def main() 
	sll = SingleLL.new()
	#  Linked list
	#  7 → 2 → 3 → 8 → 5 → 8 → NULL
	sll.addNode(8)
	sll.addNode(5)
	sll.addNode(8)
	sll.addNode(3)
	sll.addNode(2)
	sll.addNode(7)
	print(" Linked List \n")
	sll.display()
	#  723858 X 4
	#  --------------
	#  2895432   
	sll.multiplyDigit(4)
	print(" Linked List \n")
	sll.display()
end

main()

Output

 Linked List 
 7 → 2 → 3 → 8 → 5 → 8 → NULL
 Multiply By : 4
 Linked List 
 2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL
/*
    Scala program for
    Multiply a single digit to number represented as linked list
*/
// Linked list node
class LinkNode(var data: Int,
	var next: LinkNode);
class SingleLL(var head: LinkNode)
{
	def this()
	{
		this(null);
	}
	// Adding new node at beginning of linked list
	def addNode(data: Int): Unit = {
		// Create new node
		this.head = new LinkNode(data, this.head);
	}
	// Display linked list element
	def 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;
		}
		print(" NULL\n");
	}
	//reverse a single linked list
	def reverseList(): 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 reference of newly head node
			prev = this.head;
		}
	}
	// 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 temp: LinkNode = this.head;
		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
			temp = new LinkNode(product, null);
			// 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();
		// Linked list
		// 7 → 2 → 3 → 8 → 5 → 8 → NULL
		sll.addNode(8);
		sll.addNode(5);
		sll.addNode(8);
		sll.addNode(3);
		sll.addNode(2);
		sll.addNode(7);
		print(" Linked List \n");
		sll.display();
		// 723858 X 4
		// --------------
		// 2895432   
		sll.multiplyDigit(4);
		print(" Linked List \n");
		sll.display();
	}
}

Output

 Linked List
 7 → 2 → 3 → 8 → 5 → 8 → NULL
 Multiply By : 4
 Linked List
 2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL
/*
    Swift 4 program for
    Multiply a single digit to number represented as linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int, _ next: LinkNode? )
	{
		self.data = data;
		self.next = next;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	init()
	{
		self.head = nil;
	}
	// Adding new node at beginning of linked list
	func addNode(_ data: Int)
	{
		// Create new node
		self.head = LinkNode(data, self.head);
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			return;
		}
		var temp: LinkNode? = self.head;
		// iterating linked list elements
		while (temp  != nil)
		{
			print(" ", temp!.data ,"→", terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL");
	}
	//reverse a single linked list
	func reverseList()
	{
		var prev: LinkNode? = nil;
		var temp: LinkNode? = self.head;
		while (temp  != nil)
		{
			//new head node
			self.head = temp;
			//visit to next node
			temp = temp!.next;
			//change node link to pervious node
			self.head!.next = prev;
			//get the reference of newly head node
			prev = self.head;
		}
	}
	// Assuming that the linked list contains single digits of number
	// num is single digit
	func multiplyDigit(_ num: Int)
	{
		if (num > 9 || 
            num < -9 || 
            self.head == nil)
		{
			return;
		}
		print(" Multiply By : ", num);
		self.reverseList();
		// Auxiliary variables
		var temp: LinkNode? = self.head;
		var back: LinkNode? = nil;
		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
			temp = LinkNode(product, nil);
			// Add node at the last
			back!.next = temp;
		}
		// Back to its original form
		self.reverseList();
	}
}
func main()
{
	let sll: SingleLL = SingleLL();
	// Linked list
	// 7 → 2 → 3 → 8 → 5 → 8 → NULL
	sll.addNode(8);
	sll.addNode(5);
	sll.addNode(8);
	sll.addNode(3);
	sll.addNode(2);
	sll.addNode(7);
	print(" Linked List ");
	sll.display();
	// 723858 X 4
	// --------------
	// 2895432   
	sll.multiplyDigit(4);
	print(" Linked List ");
	sll.display();
}
main();

Output

 Linked List
  7 →  2 →  3 →  8 →  5 →  8 → NULL
 Multiply By :  4
 Linked List
  2 →  8 →  9 →  5 →  4 →  3 →  2 → NULL
/*
    Kotlin program for
    Multiply a single digit to number represented as linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int, next: LinkNode ? )
	{
		this.data = data;
		this.next = next;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	constructor()
	{
		this.head = null;
	}
	// Adding new node at beginning of linked list
	fun addNode(data: Int): Unit
	{
		// Create new node
		this.head = LinkNode(data, this.head);
	}
	// 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;
		}
		print(" NULL\n");
	}
	//reverse a single linked list
	fun reverseList(): 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 reference of newly head node
			prev = this.head;
		}
	}
	// 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 temp: LinkNode ? = this.head;
		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
			temp = LinkNode(product, null);
			// 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();
	// Linked list
	// 7 → 2 → 3 → 8 → 5 → 8 → NULL
	sll.addNode(8);
	sll.addNode(5);
	sll.addNode(8);
	sll.addNode(3);
	sll.addNode(2);
	sll.addNode(7);
	print(" Linked List \n");
	sll.display();
	// 723858 X 4
	// --------------
	// 2895432   
	sll.multiplyDigit(4);
	print(" Linked List \n");
	sll.display();
}

Output

 Linked List
 7 → 2 → 3 → 8 → 5 → 8 → NULL
 Multiply By : 4
 Linked List
 2 → 8 → 9 → 5 → 4 → 3 → 2 → NULL


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







© 2021, kalkicode.com, All rights reserved