Multiply two numbers represented by Linked Lists
Last Updated :
13 Sep, 2024
Given two numbers represented by linked lists, The task is to return the multiplication of these two linked lists.
Examples:
Input : head1 : 1->0->0 , head2 : 1->0
Output: 1000
Explanation: head1 represents 100 and head2 represents the number 10, 100 x 10 = 1000
Input : head1 : 3->2, head2 : 2
Output: 64
Explanation: head1 represents 32 and head2 represents the number 2, 32 x 2= 64
Approach:
To multiply two numbers represented by linked lists, you need to traverse each linked list from the head node to the end. For each linked list, initialize a variable to zero, which will be used to store the numerical value of the linked list. By processing each node, start by adding the value of the first node to this variable. For each subsequent node, multiply the variable by 10 and then add the node's value to it. This approach effectively reconstructs the number represented by the linked list. To handle potential overflow and ensure that the computations remain within manageable bounds, use the modulo operation with 1e9 + 7 after each arithmetic operation. This will keep the intermediate results within a safe range and prevent overflow during multiplication.
Below is the implementation of the above approach :
C++
// C++ program to Multiply two numbers
// represented as linked lists
#include <iostream>
using namespace std;
long long MOD = 1000000007;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Multiply contents of two linked lists
long long multiplyTwoLists(Node* first, Node* second) {
long long num1 = 0, num2 = 0;
// Traverse the first list and
// construct the number with modulo
while (first != nullptr) {
num1 = (num1 * 10 + first->data) % MOD;
first = first->next;
}
// Traverse the second list and
// construct the number with modulo
while (second != nullptr) {
num2 = (num2 * 10 + second->data) % MOD;
second = second->next;
}
// Return the product of the
// two numbers with modulo
return (num1 * num2) % MOD;
}
void printList(Node* curr) {
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
}
int main() {
// Create first list 9->4->6
Node* head1 = new Node(9);
head1->next = new Node(4);
head1->next->next = new Node(6);
// Create second list 8->4
Node* head2 = new Node(8);
head2->next = new Node(4);
cout << multiplyTwoLists(head1, head2) << endl;
return 0;
}
C
// C program to Multiply two numbers
// represented as linked lists
#include <stdlib.h>
#define MOD 1000000007
struct Node {
int data;
struct Node *next;
};
// Multiply contents of two linked lists
long long multiplyTwoLists(struct Node *first, struct Node *second) {
long long num1 = 0, num2 = 0;
// Traverse the first list and
// construct the number with modulo
while (first != NULL) {
num1 = (num1 * 10 + first->data) % MOD;
first = first->next;
}
// Traverse the second list and
// construct the number with modulo
while (second != NULL) {
num2 = (num2 * 10 + second->data) % MOD;
second = second->next;
}
// Return the product of the two
// numbers with modulo
return (num1 * num2) % MOD;
}
void printList(struct Node *curr) {
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
}
struct Node *createNode(int data) {
struct Node *newNode =
(struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
// Create first list 9->4->6
struct Node *head1 = createNode(9);
head1->next = createNode(4);
head1->next->next = createNode(6);
// Create second list 8->4
struct Node *head2 = createNode(8);
head2->next = createNode(4);
printf("%lld\n", multiplyTwoLists(head1, head2));
return 0;
}
Java
// Java program to Multiply two numbers
// represented as linked lists
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class GfG {
// Multiply contents of two linked lists
static long multiplyTwoLists(Node first, Node second) {
long MOD = 1000000007;
long num1 = 0, num2 = 0;
// Traverse the first list and
// construct the number with modulo
while (first != null || second != null) {
if (first != null) {
num1 = ((num1 * 10) + first.data) % MOD;
first = first.next;
}
// Traverse the second list and
// construct the number with modulo
if (second != null) {
num2 = ((num2 * 10) + second.data) % MOD;
second = second.next;
}
}
// Return the product of the two
// numbers with modulo
return (num1 * num2) % MOD;
}
static void printList(Node curr) {
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
}
public static void main(String args[]) {
// create first list 9->4->6
Node head1 = new Node(9);
head1.next = new Node(4);
head1.next.next = new Node(6);
// create second list 8->4
Node head2 = new Node(8);
head2.next = new Node(4);
System.out.println(multiplyTwoLists(head1, head2));
}
}
Python
# Python3 to multiply two numbers
# represented as Linked Lists
MOD = 1000000007
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Multiply contents of two linked lists
def multiplyTwoLists(first, second):
num1 = 0
num2 = 0
# Traverse the first list and
# construct the number with modulo
while first is not None:
num1 = (num1 * 10 + first.data) % MOD
first = first.next
# Traverse the second list and
# construct the number with modulo
while second is not None:
num2 = (num2 * 10 + second.data) % MOD
second = second.next
# Return the product of the two
# numbers with modulo
return (num1 * num2) % MOD
def printList(curr):
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
if __name__ == "__main__":
# Create first list 9->4->6
head1 = Node(9)
head1.next = Node(4)
head1.next.next = Node(6)
# Create second list 8->4
head2 = Node(8)
head2.next = Node(4)
print(multiplyTwoLists(head1, head2))
C#
// C++ program to Multiply two numbers
// represented as linked lists
using System;
public class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class GfG {
// Multiply contents of two linked lists
static long multiply(Node first, Node second) {
var MOD = 1000000007;
var num1 = 0;
var num2 = 0;
// Traverse the first list and
// construct the number with modulo
while (first != null || second != null) {
if (first != null) {
num1 = ((num1)*10) % MOD + first.data;
first = first.next;
}
if (second != null) {
num2 = ((num2)*10) % MOD + second.data;
second = second.next;
}
}
// Return the product of the two
// numbers with modul
return ((num1 % MOD) * (num2 % MOD)) % MOD;
}
static void printList(Node curr) {
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
}
static void Main(String[] args) {
// create first list 9->4->6
var first = new Node(9);
first.next = new Node(4);
first.next.next = new Node(6);
// create second list 8->4
var second = new Node(8);
second.next = new Node(4);
Console.WriteLine(multiply(first, second));
}
}
JavaScript
// JavaScript program to Multiply two numbers
// represented as linked lists
const MOD = 1000000007;
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Multiply contents of two linked lists
function multiplyTwoLists(first, second) {
let num1 = 0, num2 = 0;
// Traverse the first list and
// construct the number with modulo
while (first !== null) {
num1 = (num1 * 10 + first.data) % MOD;
first = first.next;
}
// Traverse the second list and
// construct the number with modulo
while (second !== null) {
num2 = (num2 * 10 + second.data) % MOD;
second = second.next;
}
// Return the product of the two
// numbers with modulo
return (num1 * num2) % MOD;
}
function printList(curr) {
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Create first list 9->4->6
let head1 = new Node(9);
head1.next = new Node(4);
head1.next.next = new Node(6);
// Create second list 8->4
let head2 = new Node(8);
head2.next = new Node(4);
console.log(multiplyTwoLists(head1, head2));
Time Complexity: O(max(n1, n2)), where n1 and n2 represents the number of nodes present in the first and second linked list respectively.
Auxiliary Space: O(1)