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>

// 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;
}
void multiplyByDigit(struct LinkNode *start, 
                     struct SingleLL *sll, 
                     int digit)
{
	struct LinkNode *temp = sll->head;
	struct LinkNode *current = start;
	struct LinkNode *back = NULL;
	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)
{
	struct SingleLL *result = newLinkedList();
	if (sll1->head == NULL || sll2->head == NULL)
	{
		return result;
	}
	// First node of resultant list
	result->head = newNode(0, NULL);
	// Reverse linked lists
	sll1->head = reverseList(sll1->head);
	sll2->head = reverseList(sll2->head);
	struct LinkNode *temp = sll2->head;
	struct LinkNode *back = result->head;
	while (temp != NULL)
	{
		multiplyByDigit(back, sll1, temp->data);
		temp = temp->next;
		back = back->next;
	}
	// Reverse linked lists
	sll1->head = reverseList(sll1->head);
	sll2->head = reverseList(sll2->head);
	result->head = reverseList(result->head);
	return result;
}
int main(int argc, char
	const *argv[])
{
	struct SingleLL *sll1 = newLinkedList();
	struct SingleLL *sll2 = newLinkedList();
	// Linked list
	// 3 → 9 → 7 → 8 → 9 → 8 → NULL
	addNode(sll1, 8);
	addNode(sll1, 9);
	addNode(sll1, 8);
	addNode(sll1, 7);
	addNode(sll1, 9);
	addNode(sll1, 3);
	// 9 → 5 → 8 → 8 → 7 → NULL
	addNode(sll2, 7);
	addNode(sll2, 8);
	addNode(sll2, 8);
	addNode(sll2, 5);
	addNode(sll2, 9);
	printf(" Linked List A \n");
	display(sll1->head);
	printf(" Linked List B \n");
	display(sll2->head);
	// 397898 *95887 = 38153245526
	// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
	struct SingleLL *sll3 = multiply(sll2, sll1);
	printf(" Linked List C \n");
	display(sll3->head);
	return 0;
}

Output

 Linked List A
 3 → 9 → 7 → 8 → 9 → 8 → NULL
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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
*/
// 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;
		}
	}
	public void multiplyByDigit(LinkNode start, int digit)
	{
		LinkNode temp = this.head;
		LinkNode current = start;
		LinkNode back = null;
		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 = new LinkNode(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 = new LinkNode(product, null);
			// 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();
		if (this.head == null || other.head == null)
		{
			return result;
		}
		// First node of resultant list
		result.head = new LinkNode(0, null);
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		LinkNode temp = other.head;
		LinkNode back = result.head;
		while (temp != null)
		{
			this.multiplyByDigit(back, temp.data);
			temp = temp.next;
			back = back.next;
		}
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		result.reverseList();
		return result;
	}
	public static void main(String[] args)
	{
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		// Linked list
		// 3 → 9 → 7 → 8 → 9 → 8 → NULL
		sll1.addNode(8);
		sll1.addNode(9);
		sll1.addNode(8);
		sll1.addNode(7);
		sll1.addNode(9);
		sll1.addNode(3);
		// 9 → 5 → 8 → 8 → 7 → NULL
		sll2.addNode(7);
		sll2.addNode(8);
		sll2.addNode(8);
		sll2.addNode(5);
		sll2.addNode(9);
		System.out.print(" Linked List A \n");
		sll1.display();
		System.out.print(" 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);
		System.out.print(" Linked List C \n");
		sll3.display();
	}
}

Output

 Linked List A
 3 → 9 → 7 → 8 → 9 → 8 → NULL
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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
*/
// 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;
		}
	}
	void multiplyByDigit(LinkNode *start, int digit)
	{
		LinkNode *temp = this->head;
		LinkNode *current = start;
		LinkNode *back = nullptr;
		int remainder = 0;
		int product = 0;
		// Iterate linked list and multiply given digit
		while (temp != nullptr)
		{
			if (current == nullptr)
			{
				// Need to create new node
				current = new LinkNode(0, nullptr);
				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 = new LinkNode(product, nullptr);
			// 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();
		if (this->head == nullptr || other->head == nullptr)
		{
			return result;
		}
		// First node of resultant list
		result->head = new LinkNode(0, nullptr);
		// Reverse linked lists
		this->reverseList();
		other->reverseList();
		LinkNode *temp = other->head;
		LinkNode *back = result->head;
		while (temp != nullptr)
		{
			this->multiplyByDigit(back, temp->data);
			temp = temp->next;
			back = back->next;
		}
		// Reverse linked lists
		this->reverseList();
		other->reverseList();
		result->reverseList();
		return result;
	}
};
int main()
{
	SingleLL *sll1 = new SingleLL();
	SingleLL *sll2 = new SingleLL();
	// Linked list
	// 3 → 9 → 7 → 8 → 9 → 8 → NULL
	sll1->addNode(8);
	sll1->addNode(9);
	sll1->addNode(8);
	sll1->addNode(7);
	sll1->addNode(9);
	sll1->addNode(3);
	// 9 → 5 → 8 → 8 → 7 → NULL
	sll2->addNode(7);
	sll2->addNode(8);
	sll2->addNode(8);
	sll2->addNode(5);
	sll2->addNode(9);
	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
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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
*/
// Linked list node
type LinkNode struct {
	data int
	next * LinkNode
}
func getLinkNode(data int, next * LinkNode) * LinkNode {
	return  &LinkNode {data,next}
}
type SingleLL struct {
	head * LinkNode
}
func getSingleLL() * SingleLL {
	return &SingleLL {nil}
}
// 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
	}
}
func(this *SingleLL) multiplyByDigit(start * LinkNode, digit int) {
	var temp * LinkNode = this.head
	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
			current = getLinkNode(0, nil)
			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 = getLinkNode(product, nil)
		// 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()
	if this.head == nil || other.head == nil {
		return result
	}
	// First node of resultant list
	result.head = getLinkNode(0, nil)
	// Reverse linked lists
	this.reverseList()
	other.reverseList()
	var temp * LinkNode = other.head
	var back * LinkNode = result.head
	for (temp != nil) {
		this.multiplyByDigit(back, temp.data)
		temp = temp.next
		back = back.next
	}
	// Reverse linked lists
	this.reverseList()
	other.reverseList()
	result.reverseList()
	return result
}
func main() {
	var sll1 * SingleLL = getSingleLL()
	var sll2 * SingleLL = getSingleLL()
	// Linked list
	// 3 → 9 → 7 → 8 → 9 → 8 → NULL
	sll1.addNode(8)
	sll1.addNode(9)
	sll1.addNode(8)
	sll1.addNode(7)
	sll1.addNode(9)
	sll1.addNode(3)
	// 9 → 5 → 8 → 8 → 7 → NULL
	sll2.addNode(7)
	sll2.addNode(8)
	sll2.addNode(8)
	sll2.addNode(5)
	sll2.addNode(9)
	fmt.Print(" Linked List A \n")
	sll1.display()
	fmt.Print(" Linked List B \n")
	sll2.display()
	// 397898 *95887 = 38153245526
	// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
	var sll3 * SingleLL = sll1.multiply(sll2)
	fmt.Print(" Linked List C \n")
	sll3.display()
}

Output

 Linked List A
 3 → 9 → 7 → 8 → 9 → 8 → NULL
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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
*/
// 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;
		}
	}
	public void multiplyByDigit(LinkNode start, int digit)
	{
		var temp = this.head;
		var current = start;
		LinkNode back = null;
		var product = 0;
		// Iterate linked list and multiply given digit
		while (temp != null)
		{
			if (current == null)
			{
				// Need to create new node
				current = new LinkNode(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 = (int)(product / 10);
			// Visit to next node
			temp = temp.next;
			current = current.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;
		}
	}
	// Assuming that the linked list contains single digits of number
	public SingleLL multiply(SingleLL other)
	{
		var result = new SingleLL();
		if (this.head == null || other.head == null)
		{
			return result;
		}
		// First node of resultant list
		result.head = new LinkNode(0, null);
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		var temp = other.head;
		var back = result.head;
		while (temp != null)
		{
			this.multiplyByDigit(back, temp.data);
			temp = temp.next;
			back = back.next;
		}
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		result.reverseList();
		return result;
	}
	public static void Main(String[] args)
	{
		var sll1 = new SingleLL();
		var sll2 = new SingleLL();
		// Linked list
		// 3 → 9 → 7 → 8 → 9 → 8 → NULL
		sll1.addNode(8);
		sll1.addNode(9);
		sll1.addNode(8);
		sll1.addNode(7);
		sll1.addNode(9);
		sll1.addNode(3);
		// 9 → 5 → 8 → 8 → 7 → NULL
		sll2.addNode(7);
		sll2.addNode(8);
		sll2.addNode(8);
		sll2.addNode(5);
		sll2.addNode(9);
		Console.Write(" Linked List A \n");
		sll1.display();
		Console.Write(" Linked List B \n");
		sll2.display();
		// 397898 *95887 = 38153245526
		// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
		var sll3 = sll1.multiply(sll2);
		Console.Write(" Linked List C \n");
		sll3.display();
	}
}

Output

 Linked List A
 3 → 9 → 7 → 8 → 9 → 8 → NULL
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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
*/
// 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;
		}
	}
	public	function multiplyByDigit($start, $digit)
	{
		$temp = $this->head;
		$current = $start;
		$back = NULL;
		$product = 0;
		// Iterate linked list and multiply given digit
		while ($temp != NULL)
		{
			if ($current == NULL)
			{
				// Need to create new node
				$current = new LinkNode(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 = (int)($product / 10);
			// Visit to next node
			$temp = $temp->next;
			$current = $current->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;
		}
	}
	// Assuming that the linked list contains single digits of number
	public	function multiply($other)
	{
		$result = new SingleLL();
		if ($this->head == NULL || $other->head == NULL)
		{
			return $result;
		}
		// First node of resultant list
		$result->head = new LinkNode(0, NULL);
		// Reverse linked lists
		$this->reverseList();
		$other->reverseList();
		$temp = $other->head;
		$back = $result->head;
		while ($temp != NULL)
		{
			$this->multiplyByDigit($back, $temp->data);
			$temp = $temp->next;
			$back = $back->next;
		}
		// Reverse linked lists
		$this->reverseList();
		$other->reverseList();
		$result->reverseList();
		return $result;
	}
	public static
	function main($args)
	{
		$sll1 = new SingleLL();
		$sll2 = new SingleLL();
		// Linked list
		// 3 → 9 → 7 → 8 → 9 → 8 → NULL
		$sll1->addNode(8);
		$sll1->addNode(9);
		$sll1->addNode(8);
		$sll1->addNode(7);
		$sll1->addNode(9);
		$sll1->addNode(3);
		// 9 → 5 → 8 → 8 → 7 → NULL
		$sll2->addNode(7);
		$sll2->addNode(8);
		$sll2->addNode(8);
		$sll2->addNode(5);
		$sll2->addNode(9);
		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
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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
*/
// 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;
		}
	}
	multiplyByDigit(start, digit)
	{
		var temp = this.head;
		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
				current = new LinkNode(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 = parseInt(product / 10);
			// Visit to next node
			temp = temp.next;
			current = current.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;
		}
	}
	// Assuming that the linked list contains single digits of number
	multiply(other)
	{
		var result = new SingleLL();
		if (this.head == null || other.head == null)
		{
			return result;
		}
		// First node of resultant list
		result.head = new LinkNode(0, null);
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		var temp = other.head;
		var back = result.head;
		while (temp != null)
		{
			this.multiplyByDigit(back, temp.data);
			temp = temp.next;
			back = back.next;
		}
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		result.reverseList();
		return result;
	}
}

function main()
{
	var sll1 = new SingleLL();
	var sll2 = new SingleLL();
	// Linked list
	// 3 → 9 → 7 → 8 → 9 → 8 → NULL
	sll1.addNode(8);
	sll1.addNode(9);
	sll1.addNode(8);
	sll1.addNode(7);
	sll1.addNode(9);
	sll1.addNode(3);
	// 9 → 5 → 8 → 8 → 7 → NULL
	sll2.addNode(7);
	sll2.addNode(8);
	sll2.addNode(8);
	sll2.addNode(5);
	sll2.addNode(9);
	process.stdout.write(" Linked List A \n");
	sll1.display();
	process.stdout.write(" Linked List B \n");
	sll2.display();
	// 397898 *95887 = 38153245526
	// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
	var sll3 = sll1.multiply(sll2);
	process.stdout.write(" Linked List C \n");
	sll3.display();
}
// Start program execution
main();

Output

 Linked List A
 3 → 9 → 7 → 8 → 9 → 8 → NULL
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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


#  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
		
	
	def multiplyByDigit(self, start, digit) :
		temp = self.head
		current = start
		back = None
		product = 0
		#  Iterate linked list and multiply given digit
		while (temp != None) :
			if (current == None) :
				#  Need to create new node
				current = LinkNode(0, None)
				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
			temp = LinkNode(product, None)
			#  Add node at the last
			back.next = temp
		
	
	#  Assuming that the linked list contains single digits of number
	def multiply(self, other) :
		result = SingleLL()
		if (self.head == None or other.head == None) :
			return result
		
		#  First node of resultant list
		result.head = LinkNode(0, None)
		#  Reverse linked lists
		self.reverseList()
		other.reverseList()
		temp = other.head
		back = result.head
		while (temp != None) :
			self.multiplyByDigit(back, temp.data)
			temp = temp.next
			back = back.next
		
		#  Reverse linked lists
		self.reverseList()
		other.reverseList()
		result.reverseList()
		return result
	

def main() :
	sll1 = SingleLL()
	sll2 = SingleLL()
	#  Linked list
	#  3 → 9 → 7 → 8 → 9 → 8 → NULL
	sll1.addNode(8)
	sll1.addNode(9)
	sll1.addNode(8)
	sll1.addNode(7)
	sll1.addNode(9)
	sll1.addNode(3)
	#  9 → 5 → 8 → 8 → 7 → NULL
	sll2.addNode(7)
	sll2.addNode(8)
	sll2.addNode(8)
	sll2.addNode(5)
	sll2.addNode(9)
	print(" Linked List A ")
	sll1.display()
	print(" Linked List B ")
	sll2.display()
	#  397898 *95887 = 38153245526
	#  3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
	sll3 = sll1.multiply(sll2)
	print(" Linked List C ")
	sll3.display()

if __name__ == "__main__": main()

Output

 Linked List A
 3 → 9 → 7 → 8 → 9 → 8 → NULL
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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

#  Linked list node
class LinkNode 
	# Define the accessor and reader of class LinkNode
	attr_reader :data, :next
	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
	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

	def multiplyByDigit(start, digit) 
		temp = self.head
		current = start
		back = nil
		product = 0
		#  Iterate linked list and multiply given digit
		while (temp != nil) 
			if (current == nil) 
				#  Need to create new node
				current = LinkNode.new(0, nil)
				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
			temp = LinkNode.new(product, nil)
			#  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()
		if (self.head == nil || other.head == nil) 
			return result
		end

		#  First node of resultant list
		result.head = LinkNode.new(0, nil)
		#  Reverse linked lists
		self.reverseList()
		other.reverseList()
		temp = other.head
		back = result.head
		while (temp != nil) 
			self.multiplyByDigit(back, temp.data)
			temp = temp.next
			back = back.next
		end

		#  Reverse linked lists
		self.reverseList()
		other.reverseList()
		result.reverseList()
		return result
	end

end

def main() 
	sll1 = SingleLL.new()
	sll2 = SingleLL.new()
	#  Linked list
	#  3 → 9 → 7 → 8 → 9 → 8 → NULL
	sll1.addNode(8)
	sll1.addNode(9)
	sll1.addNode(8)
	sll1.addNode(7)
	sll1.addNode(9)
	sll1.addNode(3)
	#  9 → 5 → 8 → 8 → 7 → NULL
	sll2.addNode(7)
	sll2.addNode(8)
	sll2.addNode(8)
	sll2.addNode(5)
	sll2.addNode(9)
	print(" Linked List A \n")
	sll1.display()
	print(" 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)
	print(" Linked List C \n")
	sll3.display()
end

main()

Output

 Linked List A
 3 → 9 → 7 → 8 → 9 → 8 → NULL
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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
*/
// 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;
		}
	}
	def multiplyByDigit(start: LinkNode, digit: Int): Unit = {
		var temp: LinkNode = this.head;
		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
				current = new LinkNode(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 = new LinkNode(product, null);
			// 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();
		if (this.head == null || other.head == null)
		{
			return result;
		}
		// First node of resultant list
		result.head = new LinkNode(0, null);
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		var temp: LinkNode = other.head;
		var back: LinkNode = result.head;
		while (temp != null)
		{
			this.multiplyByDigit(back, temp.data);
			temp = temp.next;
			back = back.next;
		}
		// Reverse linked lists
		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();
		// Linked list
		// 3 → 9 → 7 → 8 → 9 → 8 → NULL
		sll1.addNode(8);
		sll1.addNode(9);
		sll1.addNode(8);
		sll1.addNode(7);
		sll1.addNode(9);
		sll1.addNode(3);
		// 9 → 5 → 8 → 8 → 7 → NULL
		sll2.addNode(7);
		sll2.addNode(8);
		sll2.addNode(8);
		sll2.addNode(5);
		sll2.addNode(9);
		print(" Linked List A \n");
		sll1.display();
		print(" Linked List B \n");
		sll2.display();
		// 397898 *95887 = 38153245526
		// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
		var sll3: SingleLL = sll1.multiply(sll2);
		print(" Linked List C \n");
		sll3.display();
	}
}

Output

 Linked List A
 3 → 9 → 7 → 8 → 9 → 8 → NULL
 Linked List B
 9 → 5 → 8 → 8 → 7 → NULL
 Linked List C
 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
*/
// 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\n", terminator: "");
	}
	//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;
		}
	}
	func multiplyByDigit(_ start: LinkNode? , _ digit : Int)
	{
		var temp: LinkNode? = self.head;
		var current: LinkNode? = start;
		var back: LinkNode? = nil;
		var product: Int = 0;
		// Iterate linked list and multiply given digit
		while (temp  != nil)
		{
			if (current == nil)
			{
				// Need to create new node
				current = LinkNode(0, nil);
				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 = LinkNode(product, nil);
			// 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();
		if (self.head == nil || other!.head == nil)
		{
			return result;
		}
		// First node of resultant list
		result!.head = LinkNode(0, nil);
		// Reverse linked lists
		self.reverseList();
		other!.reverseList();
		var temp: LinkNode? = other!.head;
		var back: LinkNode? = result!.head;
		while (temp  != nil)
		{
			self.multiplyByDigit(back, temp!.data);
			temp = temp!.next;
			back = back!.next;
		}
		// Reverse linked lists
		self.reverseList();
		other!.reverseList();
		result!.reverseList();
		return result;
	}
	static func main(_ args: [String])
	{
		let sll1: SingleLL? = SingleLL();
		let sll2: SingleLL? = SingleLL();
		// Linked list
		// 3 → 9 → 7 → 8 → 9 → 8 → NULL
		sll1!.addNode(8);
		sll1!.addNode(9);
		sll1!.addNode(8);
		sll1!.addNode(7);
		sll1!.addNode(9);
		sll1!.addNode(3);
		// 9 → 5 → 8 → 8 → 7 → NULL
		sll2!.addNode(7);
		sll2!.addNode(8);
		sll2!.addNode(8);
		sll2!.addNode(5);
		sll2!.addNode(9);
		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
 Linked List B
9 → 5 → 8 → 8 → 7 →  NULL
 Linked List C
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
*/
// 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;
		}
	}
	fun multiplyByDigit(start: LinkNode ? , digit : Int): Unit
	{
		var temp: LinkNode ? = this.head;
		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
				current = LinkNode(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 = LinkNode(product, null);
			// 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();
		if (this.head == null || other.head == null)
		{
			return result;
		}
		// First node of resultant list
		result.head = LinkNode(0, null);
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		var temp: LinkNode ? = other.head;
		var back: LinkNode ? = result.head;
		while (temp != null)
		{
			this.multiplyByDigit(back, temp.data);
			temp = temp.next;
			back = back?.next;
		}
		// Reverse linked lists
		this.reverseList();
		other.reverseList();
		result.reverseList();
		return result;
	}
}
fun main(args: Array < String > ): Unit
{
	val sll1: SingleLL = SingleLL();
	val sll2: SingleLL = SingleLL();
	// Linked list
	// 3 → 9 → 7 → 8 → 9 → 8 → NULL
	sll1.addNode(8);
	sll1.addNode(9);
	sll1.addNode(8);
	sll1.addNode(7);
	sll1.addNode(9);
	sll1.addNode(3);
	// 9 → 5 → 8 → 8 → 7 → NULL
	sll2.addNode(7);
	sll2.addNode(8);
	sll2.addNode(8);
	sll2.addNode(5);
	sll2.addNode(9);
	print(" Linked List A \n");
	sll1.display();
	print(" Linked List B \n");
	sll2.display();
	// 397898 *95887 = 38153245526
	// 3 → 8 → 1 → 5 → 3 → 2 → 4 → 5 → 5 → 2 → 6 → NULL
	val sll3: SingleLL = sll1.multiply(sll2);
	print(" Linked List C \n");
	sll3.display();
}

Output

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