Multiply two numbers represented by Linked Lists

Last Updated : 26 Jul, 2026

Given the heads of two singly linked lists L1 and L2, where each node stores a single digit of a non-negative integer, return the product of the two numbers represented by the linked lists. Since the answer can be very large, return it modulo 10^9 + 7.

Note: The digits are stored in the same order as the number (most significant digit first).

Examples: 

Input: L1 = 3 -> 2 , L2 = 2
Output: 64
Explanation: Multiplication of 32 and 2 gives 64.

231

Input: L1 = 1 -> 0 -> 0, L2 = 1 -> 0
Output: 1000
Explanation: Multiplication of 100 and 10 gives 1000.

232
Try It Yourself
redirect icon

[Naive Approach] Constructing the Complete Numbers - O(n + m) Time and O(1) Space

The idea is to traverse both linked lists and construct the complete numbers they represent. Once the two numbers are obtained, multiply them and return the product modulo 10^9 + 7.

  • Initialize two variables, num1 and num2, to store the numbers represented by the two linked lists.
  • Traverse the first linked list and construct num1 by appending each digit (num1 = num1 × 10 + digit).
  • Traverse the second linked list and construct num2 in the same manner.
  • Multiply the two constructed numbers.
  • Return the product modulo 10^9 + 7.
C++
#include <iostream>
using namespace std;

// Structure of a linked list node
class Node
{
  public:
    int data;
    Node *next;

    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

// Function to multiply two numbers represented by linked lists
int multiplyTwoLists(Node *first, Node *second)
{
    const long long MOD = 1000000007;
    
    // Variables to store the numbers represented by the linked lists
    long long num1 = 0, num2 = 0;

    // Traverse the first linked list and construct the first number
    while (first != nullptr)
    {
        num1 = num1 * 10 + first->data;
        first = first->next;
    }

    // Traverse the second linked list and construct the second number
    while (second != nullptr)
    {
        num2 = num2 * 10 + second->data;
        second = second->next;
    }

    // Return the product modulo MOD
    // Note: This multiplication may overflow for very large numbers.
    return (num1 * num2) % MOD;
}

int main()
{
    // Create first linked list: 9 -> 4 -> 6
    Node *first = new Node(9);
    first->next = new Node(4);
    first->next->next = new Node(6);

    // Create second linked list: 8 -> 4
    Node *second = new Node(8);
    second->next = new Node(4);

    cout << multiplyTwoLists(first, second) << endl;

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {

    static final long MOD = 1000000007;

    // Function to multiply two numbers represented by
    // linked lists
    static long multiplyTwoLists(Node first, Node second)
    {

        // Variables to store the numbers represented by the
        // linked lists
        long num1 = 0, num2 = 0;

        // Traverse the first linked list and construct the
        // first number
        while (first != null) {
            num1 = num1 * 10 + first.data;
            first = first.next;
        }

        // Traverse the second linked list and construct the
        // second number
        while (second != null) {
            num2 = num2 * 10 + second.data;
            second = second.next;
        }

        // Return the product modulo MOD
        // Note: This multiplication may overflow for very
        // large numbers.
        return (num1 * num2) % MOD;
    }

    public static void main(String[] args)
    {
        // Create first linked list: 9 -> 4 -> 6
        Node first = new Node(9);
        first.next = new Node(4);
        first.next.next = new Node(6);

        // Create second linked list: 8 -> 4
        Node second = new Node(8);
        second.next = new Node(4);

        System.out.println(multiplyTwoLists(first, second));
    }
}
Python
# Structure of a linked list node
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


# Function to multiply two numbers represented by linked lists
def multiplyTwoLists(first, second):
    MOD = 1000000007

    # Variables to store the numbers represented by the linked lists
    num1, num2 = 0, 0

    # Traverse the first linked list and construct the first number
    while first:
        num1 = num1 * 10 + first.data
        first = first.next

    # Traverse the second linked list and construct the second number
    while second:
        num2 = num2 * 10 + second.data
        second = second.next

    # Return the product modulo MOD
    # Note: Python integers do not overflow, but this is the naive approach.
    return (num1 * num2) % MOD


# Driver code
if __name__ == "__main__":
    # Create first linked list: 9 -> 4 -> 6
    first = Node(9)
    first.next = Node(4)
    first.next.next = Node(6)

    # Create second linked list: 8 -> 4
    second = Node(8)
    second.next = Node(4)

    print(multiplyTwoLists(first, second))
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {
    static long MOD = 1000000007;

    // Function to multiply two numbers represented by
    // linked lists
    static long multiplyTwoLists(Node first, Node second)
    {
        // Variables to store the numbers represented by the
        // linked lists
        long num1 = 0, num2 = 0;

        // Traverse the first linked list and construct the
        // first number
        while (first != null) {
            num1 = num1 * 10 + first.data;
            first = first.next;
        }

        // Traverse the second linked list and construct the
        // second number
        while (second != null) {
            num2 = num2 * 10 + second.data;
            second = second.next;
        }

        // Return the product modulo MOD
        // Note: This multiplication may overflow for very
        // large numbers.
        return (num1 * num2) % MOD;
    }

    static void Main()
    {
        // Create first linked list: 9 -> 4 -> 6
        Node first = new Node(9);
        first.next = new Node(4);
        first.next.next = new Node(6);

        // Create second linked list: 8 -> 4
        Node second = new Node(8);
        second.next = new Node(4);
        
        Console.WriteLine(multiplyTwoLists(first, second));
    }
}
JavaScript
// Structure of a linked list node
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

// Function to multiply two numbers represented by linked
// lists
function multiplyTwoLists(first, second)
{
    const MOD = 1000000007n;

    // Variables to store the numbers represented by the
    // linked lists
    let num1 = 0n;
    let num2 = 0n;

    // Traverse the first linked list and construct the
    // first number
    while (first !== null) {
        num1 = num1 * 10n + BigInt(first.data);
        first = first.next;
    }

    // Traverse the second linked list and construct the
    // second number
    while (second !== null) {
        num2 = num2 * 10n + BigInt(second.data);
        second = second.next;
    }

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

// Driver code

// Create first linked list: 9 -> 4 -> 6
let first = new Node(9);
first.next = new Node(4);
first.next.next = new Node(6);

// Create second linked list: 8 -> 4
let second = new Node(8);
second.next = new Node(4);

console.log(multiplyTwoLists(first, second));

Output
79464

[Expected Approach] Using Modular Arithmetic - O(n + m) Time and O(1) Space

The numbers represented by the linked lists can be extremely large, making it impossible to store them in built-in integer types without overflow. Instead of constructing the complete numbers, we compute their values modulo 10^9 + 7 while traversing the linked lists. So, while forming each number, we repeatedly take modulo to keep the intermediate values within range.

  • Initialize two variables num1 and num2 to 0.
  • Traverse the first linked list and update num1 as num1 = (num1 × 10 + digit) % MOD.
  • Traverse the second linked list and similarly compute num2.
  • Multiply num1 and num2, and take the result modulo MOD.
  • Return the final product.
C++
#include <iostream>
using namespace std;

// Structure of a linked list node
class Node
{
  public:
    int data;
    Node *next;

    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

static const int MOD = 1000000007;

// Function to convert a linked list into a number modulo MOD
long long getNumber(Node *head)
{
    long long num = 0;

    // Traverse the linked list and compute its value modulo MOD
    while (head != nullptr)
    {
        num = (num * 10 + head->data) % MOD;
        head = head->next;
    }

    return num;
}

int multiplyTwoLists(Node *first, Node *second)
{
    // Get the numbers represented by the linked lists modulo MOD
    long long num1 = getNumber(first);
    long long num2 = getNumber(second);

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

int main()
{
    // Create first linked list: 9 -> 4 -> 6
    Node *first = new Node(9);
    first->next = new Node(4);
    first->next->next = new Node(6);

    // Create second linked list: 8 -> 4
    Node *second = new Node(8);
    second->next = new Node(4);

    cout << multiplyTwoLists(first, second) << endl;

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {

    static final int MOD = 1000000007;

    // Function to convert a linked list into a number
    // modulo MOD
    static long getNumber(Node head)
    {
        // Variable to store the number modulo MOD
        long num = 0;

        // Traverse the linked list and compute its value
        // modulo MOD
        while (head != null) {
            num = (num * 10 + head.data) % MOD;
            head = head.next;
        }

        return num;
    }

    // Function to multiply two numbers represented by
    // linked lists
    static long multiplyTwoLists(Node first, Node second)
    {
        // Get the numbers represented by the linked lists
        // modulo MOD
        long num1 = getNumber(first);
        long num2 = getNumber(second);

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

    public static void main(String[] args)
    {
        Node first = new Node(9);
        first.next = new Node(4);
        first.next.next = new Node(6);

        Node second = new Node(8);
        second.next = new Node(4);

        System.out.println(multiplyTwoLists(first, second));
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


# Function to convert a linked list into a number modulo MOD
def getNumber(head, MOD):

    # Variable to store the number modulo MOD
    num = 0

    # Traverse the linked list and compute its value modulo MOD
    while head:
        num = (num * 10 + head.data) % MOD
        head = head.next

    return num


# Function to multiply two numbers represented by linked lists
def multiplyTwoLists(first, second):
    MOD = 1000000007

    # Get the numbers represented by the linked lists modulo MOD
    num1 = getNumber(first, MOD)
    num2 = getNumber(second, MOD)

    # Return the product modulo MOD
    return (num1 * num2) % MOD


# Driver Code
if __name__ == "__main__":
    first = Node(9)
    first.next = Node(4)
    first.next.next = Node(6)

    second = Node(8)
    second.next = Node(4)

    print(multiplyTwoLists(first, second))
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {
    static long MOD = 1000000007;

    // Function to convert a linked list into a number
    // modulo MOD
    static long GetNumber(Node head)
    {
        // Variable to store the number modulo MOD
        long num = 0;

        // Traverse the linked list and compute its value
        // modulo MOD
        while (head != null) {
            num = (num * 10 + head.data) % MOD;
            head = head.next;
        }

        return num;
    }

    // Function to multiply two numbers represented by
    // linked lists
    static long multiplyTwoLists(Node first, Node second)
    {
        // Get the numbers represented by the linked lists
        // modulo MOD
        long num1 = GetNumber(first);
        long num2 = GetNumber(second);

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

    static void Main()
    {
        Node first = new Node(9);
        first.next = new Node(4);
        first.next.next = new Node(6);

        Node second = new Node(8);
        second.next = new Node(4);

        Console.WriteLine(multiplyTwoLists(first, second));
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

// Function to convert a linked list into a number modulo
// MOD
function getNumber(head, MOD)
{
    // Variable to store the number modulo MOD
    let num = 0n;

    // Traverse the linked list and compute its value modulo
    // MOD
    while (head !== null) {
        num = (num * 10n + BigInt(head.data)) % MOD;
        head = head.next;
    }

    return num;
}

// Function to multiply two numbers represented by linked
// lists
function multiplyTwoLists(first, second)
{
    const MOD = 1000000007n;
    
    // Get the numbers represented by the linked lists
    // modulo MOD
    const num1 = getNumber(first, MOD);
    const num2 = getNumber(second, MOD);

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

// Driver Code

let first = new Node(9);
first.next = new Node(4);
first.next.next = new Node(6);

let second = new Node(8);
second.next = new Node(4);

console.log(multiplyTwoLists(first, second));

Output
79464
Comment