Multiply two linked lists | GeeksForGeeks | Problem of the Day

preview_player
Показать описание
Given elements as nodes of the two singly linked lists. The task is to multiply these two linked lists, say L1 and L2.

Note: The output could be large take modulo 10^9+7.

Examples :

Input: LinkedList L1 : 3-2 , LinkedList L2 : 2
Output: 64
Explanation:

Multiplication of 32 and 2 gives 64.
Input: LinkedList L1: 1-0-0 , LinkedList L2 : 1-0
Output: 1000
Explanation:

Multiplication of 100 and 10 gives 1000.
Expected Time Complexity: O(max(n,m))
Expected Auxilliary Space: O(1)
where n is the size of L1 and m is the size of L2

#geeksforgeeks
#problemoftheday
#education
#computerscience
#coding
#array
#sorting
#sorts
#sort
#datastructure
#algorithmicproblemsolving
#algorithms
#algorithm
#dynamicprogramming
#dp
#potd
#gfg
#binarytree
#binarysearchtree
#bst
#string
#dictionary
#python
#stack
#queue
#python
#c++
#interview

Table of Contents
0:00 Problem Statement
0:33 Solution
2:10 Pseudo Code
3:26 Code - Python
4:10 Code - C++
Рекомендации по теме
Комментарии
Автор

Table of Contents
0:00 Problem Statement
0:33 Solution
2:10 Pseudo Code
3:26 Code - Python
4:10 Code - C++

mathematics
Автор

class Solution:
MOD =
def to_number(self, head):
num = 0

while head:
num = (num*10 + head.data)%self.MOD
head = head.next;

return num

def multiply_two_lists(self, first, second):
num1 = self.to_number(first)
num2 = self.to_number(second)

return (num1*num2)%self.MOD

mathematics
Автор

class solution {
public:
int MOD =
long long to_number(Node *head) {
long long num = 0;

while (head) {
num = (num*10 + head->data)%MOD;
head = head->next;
}

return num;
}

long long multiplyTwoLists(Node *first, Node *second) {
long long num1 = 0, num2 = 0;
long long ans = 0;

num1 = to_number(first);
num2 = to_number(second);

return (num1*num2)%MOD;
}
};

mathematics