Multiply two linked lists

Multiply two linked lists

GeeksforGeeks

Multiplying Linked Lists for Massive Numbers: Learn with O(max(n, m)) Time Complexity! ????


???? Problem:

Given two singly linked lists representing large numbers, the task is to multiply them. The result could be quite large, so we'll take modulo 109+710^9 + 7109+7.

?? Examples:

Input:

  • LinkedList L1: 3 → 2
  • LinkedList L2: 2
  • Output: 64
  • Explanation: Multiplying 32 by 2 gives 64.

Input:

  • LinkedList L1: 1 → 0 → 0
  • LinkedList L2: 1 → 0
  • Output: 1000
  • Explanation: Multiplying 100 by 10 gives 1000.


?? Approach:

In this problem, we need to simulate the process of multiplying two large numbers stored in linked lists, where each node represents a single digit. Here's how we can solve it efficiently.

Steps:

  1. Step 1: Extract the numbers from both linked lists and convert them into their respective integer values.
  2. Step 2: Multiply the two numbers.
  3. Step 3: Return the product modulo 109+710^9 + 7109+7.

? Optimized Time Complexity: We achieve O(max(n, m)) time complexity by only traversing each list once to construct the numbers.

class Node 
{
int data;
Node next:

Node(int data)
{
this.data = data;
next = null;
}
}
class Solution
{
// Function to multiply two linked lists.
public long multiplyTwoLists(Node first, Node second)
{
long num1 = 0;
long num2 = 0;
long MOD = 1000000007;

// Extract first number from list L1
while(first != null)
{
num1 = (num1 * 10 + first.data)%M0D;
first = first.next;
}
// Extract second number from list L2
while(second != null)
{
num2 = (num2 * 10 + second.data) %MOD;
second = second.next;
}
// Multiply the two numbers and take modulo
return( num1 * num2 ) % MOD;
}
}        

?? Time Complexity:

  • O(max(n, m)), where n is the number of nodes in list L1 and m in list L2.
  • Auxiliary Space: O(1) (Constant space, as we’re not using any extra space apart from variables).


?? Summary:

This problem demonstrates how linked lists can be used to represent large numbers and how efficiently we can manipulate them by converting the list into integers, multiplying them, and using modular arithmetic to manage large results.



要查看或添加评论,请登录

Pathan Ismailkhan的更多文章

  • Array Subset

    Array Subset

    ?? Problem: You're given two arrays: a1[] and a2[], where both may contain duplicates, and the goal is to determine…

  • Intersection Point in Linked Lists

    Intersection Point in Linked Lists

    When working with linked lists, finding where two lists intersect can be tricky especially when efficiency is crucial!…

  • Is Linked List Length Even?

    Is Linked List Length Even?

    To solve the problem of determining if the length of a linked list is even or odd, let's consider an efficient approach…

  • Count Linked List Nodes

    Count Linked List Nodes

    ?? Problem: Today’s challenge is a fundamental one in data structures—finding the length of a Singly Linked List. ??…

  • Two Smallests in Every Subarray

    Two Smallests in Every Subarray

    ? Short Summary: In today's CODE OF THE DAY, we tackle how to find the maximum sum of the two smallest elements in…

  • Reorganize The Array

    Reorganize The Array

    ?? Summary: In today’s "Code of the Day," we explore an exciting problem: rearranging an array so that arr[i] = i. ??…

  • Max distance between same elements

    Max distance between same elements

    ?? Summary: In today’s "Code of the Day," we tackle a classic problem: finding the maximum distance between repeated…

    3 条评论
  • Largest Pair Sum

    Largest Pair Sum

    To solve the problem of finding the largest pair sum in an array of distinct integers, we can utilize a simple and…

  • Not a subset sum

    Not a subset sum

    ????? Problem: Given a sorted array of positive integers, find the smallest positive integer that cannot be represented…

  • 2491. Divide Players Into Teams of Equal Skill

    2491. Divide Players Into Teams of Equal Skill

    ????? Problem: You are given an even-length array of players' skills and the goal is to divide them into teams of 2…

社区洞察

其他会员也浏览了