Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Python Program To Multiply Two Numbers Represented By Linked Lists

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Given two numbers represented by linked lists, write a function that returns the multiplication of these two linked lists.


Input: 9->4->6
Output: 79464

Input: 3->2->1
Output: 3852

Traverse both lists and generate the required numbers to be multiplied and then return the multiplied values of the two numbers. 
Algorithm to generate the number from linked list representation: 

1) Initialize a variable to zero
2) Start traversing the linked list
3) Add the value of the first node to this variable
4) From the second node, multiply the variable by 10
   and also take the modulus of this value by 10^9+7
   and then add the value of the node to this 
5) Repeat step 4 until we reach the last node of the list. 

Use the above algorithm with both linked lists to generate the numbers. 

Below is the program for multiplying two numbers represented as linked lists:  


# Python3 to multiply two numbers
# represented as Linked Lists
# Linked list node class
class Node:
    # Function to initialize the node 
    def __init__(self, data):       = data = None
# Linked List Class
class LinkedList:
    # Function to initialize the
    # LinkedList class.
    def __init__(self):
        # Initialize head as None
        self.head = None
    # Function to insert a node at the
    # beginning of the Linked List
    def push(self, new_data):
        # Create a new Node
        new_node = Node(new_data)
        # Make next of the new Node
        # as head = self.head
        # Move the head to point to
        # new Node
        self.head = new_node
    # Function to print the Linked
    # List
    def printList(self):       
        ptr = self.head 
        while (ptr != None):
                  end = '')
            if != None:
                      end = '')               
            ptr =
# Multiply contents of two Linked
# Lists
def multiplyTwoLists(first, second): 
    num1 = 0
    num2 = 0
    first_ptr = first.head
    second_ptr = second.head
    while first_ptr != None or second_ptr != None:
        if first_ptr != None:
            num1 = (num1 * 10) +
            first_ptr =
        if second_ptr != None:
            num2 = (num2 * 10) +
            second_ptr =
    return num1 * num2
# Driver code
if __name__=='__main__':
    first = LinkedList()
    second = LinkedList()
    # Create first Linked List 9->4->6
    # Printing first Linked List
    print("First list is: ", end = '')
    # Create second Linked List 8->4
    # Printing second Linked List
    print("Second List is: ",
           end = '')
    # Multiply two linked list and
    # print the result
    result = multiplyTwoLists(first,
    print("Result is: ", result)
# This code is contributed by kirtishsurangalikar


First List is: 9->4->6
Second List is: 8->4
Result is: 79464

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), no extra space is required, so it is a constant.

Please refer complete article on Multiply two numbers represented by Linked Lists for more details!

My Personal Notes arrow_drop_up
Last Updated : 15 Jun, 2022
Like Article
Save Article
Similar Reads
Related Tutorials