Multiply two linked lists | GFG POTD 1st Oct 2024 | JAVA | C++

preview_player
Показать описание
#gfgpotd #gfgpotdtoday #potd #potdgfg #potdgfgtoday #gfgproblemoftheday #problemoftheday #gfgtoday
Рекомендации по теме
Комментарии
Автор

C++ Code :
long long multiplyTwoLists(Node *first, Node *second) {
// code here
long long num1 = 0;
long long num2 = 0;
int mod =

// Traverse both linked lists and calculate the numbers
while (first != nullptr && second != nullptr) {
num1 = ((num1 * 10) + first->data) % mod;
num2 = ((num2 * 10) + second->data) % mod;
first = first->next;
second = second->next;
}

// Traverse the remaining part of the first list, if any
while (first != nullptr) {
num1 = ((num1 * 10) + first->data) % mod;
first = first->next;
}

// Traverse the remaining part of the second list, if any
while (second != nullptr) {
num2 = ((num2 * 10) + second->data) % mod;
second = second->next;
}

// Return the product modulo mod
return (num1 * num2) % mod;
}

codeCraft
Автор

JAVA Code :
public long multiplyTwoLists(Node first, Node second) {
// Code here
long num1 = 0;
long num2 = 0;
int mod =
while(first != null && second != null){
num1 = ((num1 * 10) + first.data) % mod;
num2 = ((num2 * 10) + second.data) % mod;
first = first.next;
second = second.next;
}
while(first != null){
num1 = ((num1 * 10) + first.data) % mod;
first = first.next;
}

while(second != null){
num2 = ((num2 * 10) + second.data) % mod;
second = second.next;
}

return (num1 * num2) % mod;

}

codeCraft