Multiply two numbers represented by Linked Lists

1.9

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

Examples:

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

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

Solution:
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 first node to this variable
4) From the second node, multiply the variable by 10
   first and then add the value of the node to this 
   variable.
5) Repeat step 4 until we reach the last node of the list. 

Use the above algorithm with both of linked lists to generate the numbers.
Below is the complete C program for multiplying two numbers represented as linked lists:

// C program to Multiply two numbers
// represented as linked lists
#include<stdio.h>
#include<stdlib.h>

// Linked list node
struct node
{
    int data;
    struct node* next;
};

// Function to create a new node 
// with given data
struct node *newNode(int data)
{
    struct node *new_node = (struct node *) malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

// Function to insert a node at the 
// beginning of the Linked List
void push(struct node** head_ref, int new_data)
{
    // allocate node 
    struct node* new_node = newNode(new_data);

    // link the old list off the new node 
    new_node->next = (*head_ref);

    // move the head to point to the new node 
    (*head_ref) = new_node;
}

// Multiply contents of two linked lists
long multiplyTwoLists (struct node* first, struct node* second)
{
    int num1 = 0, num2 = 0;
    
    // Generate numbers from linked lists
    while (first || second)
    {
        if (first)
        {
            num1 = num1*10 + first->data;
            first = first->next;
        }
        if (second)
        {
            num2 = num2*10 + second->data;
            second = second->next;
        }
    }

    // Return multiplication of 
    // two numbers
    return num1*num2;
}

// A utility function to print a linked list
void printList(struct node *node)
{
    while(node != NULL)
    {
        printf("%d", node->data);
        if(node->next)
            printf("->");
        node = node->next;
    }
    printf("\n");
}

// Driver program to test above function
int main(void)
{
    struct node* first = NULL;
    struct node* second = NULL;

    // create first list 9->4->6
    push(&first, 6);
    push(&first, 4);
    push(&first, 9);
    printf("First List is: ");
    printList(first);

    // create second list 8->4
    push(&second, 4);
    push(&second, 8);
    printf("Second List is: ");
    printList(second);

    // Multiply the two lists and see result
    printf("Result is: ");
    printf("%ld", multiplyTwoLists(first, second));

    return 0;
}

Output:

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

Asked in: Amazon

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.9 Average Difficulty : 1.9/5.0
Based on 11 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.