Ask a Question

 

Start Coding Today

   

GeeksQuiz

GeeksforGeeksIDE

Data Structures

Algorithms

Interview Experiences

C Programming

C++ Programming

Java Programming

GATE CS

Books

Multiply two numbers represented by linked lists.

asked Mar 1, 2016 by Shubham Vats

4 Answers

+1 vote
#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 Doubly 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;
	while (first || second)
	{
		if(first){
			num1 = num1*10 + first->data;
			first = first->next;
		}
		if(second)
		{
			num2 = num2*10 + second->data;
			second = second->next;
		}
	}


	return num1*num2;
}

// A utility function to print a linked list
void printList(struct node *node)
{
	while(node != NULL)
	{
		printf("%d ", node->data);
		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 7->5->9
	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("%d", multiplyTwoLists(first, second));

	return 0;
}

 

answered Mar 1, 2016 by aditya.goel

You should add the condition (modulo 10^9+7) for large values.

+1 vote
node* reverse(node* l){
    node* p1,*p2,*p3;
    p1=NULL;
    p2=l;
    while(p2){
        p3=p2->next;
        p2->next=p1;
        p1=p2;
        p2=p3;
    }
    return p1;
}
node* add(node* mul1,node* mul2,int k){
    node* n,*temp,*result;
    int num,carry;
    while(k){
        node* n=new node();
        n->data=0;
        n->next=mul2;
        mul2=n;
        k--;
    }
    carry=0;
    result=NULL;
    while(mul1 || mul2 ){
        num=0;
        if(mul1){
            num+=mul1->data;
            mul1=mul1->next;
            
        }
        if(mul2){
            num+=mul2->data;
            mul2=mul2->next;
        }
        num+=carry;
        if(num>9){
            carry=num/10;
            num=num%10;
        }
        else{
            carry=0;
        }
        n=new node();
        n->data=num;
        if(result==NULL){
            result=n;
            temp=n;
        }
        else{
            temp->next=n;
            temp=n;
        }
    }
    if(carry){
        n=new node();
        n->data=carry;
        temp->next=n;
        temp=n;
    }
    temp->next=NULL;
    return result;
}
/*You are required to complete this method*/
long long  multiplyTwoLists (node* l1, node* l2)
{
  //Your code here
  node* n,*temp1,*temp2,*temp,*mul1,*mul2;
  int num,k;
  temp1=reverse(l1);
  temp2=reverse(l2);
  node* cur1=temp1;
  mul1=NULL;
  int carry=0;
  while(cur1){
      num=temp2->data*cur1->data+carry;//cout<<num<<" ";
      if(num/10>=1){
          n=new node();
          n->data=num%10;
          if(mul1==NULL){
              mul1=n;
              temp=n;
          }
          else{
              temp->next=n;
              temp=n;
          }
          carry=num/10;
      }
      else{
          carry=0;
          n=new node();
          n->data=num;
          if(mul1==NULL){
              mul1=n;
              temp=n;
          }
          else{
              temp->next=n;
              temp=n;
          }
      }
      cur1=cur1->next;
  }
  if(carry){
      n=new node();
      n->data=carry;
      temp->next=n;
      temp=n;
  }
  temp->next=NULL;
  temp2=temp2->next;
  k=1;
  while(temp2){
      
      cur1=temp1;
      mul2=NULL;
      carry=0;
      while(cur1){
          num=temp2->data*cur1->data+carry;
          if(num>9){
              n=new node();
              n->data=num%10;
              if(mul2==NULL){
                mul2=n;
                temp=n;
              }
              else{
                temp->next=n;
                temp=n;
              }
              carry=num/10;
          }
          else{
              carry=0;
              n=new node();
              n->data=num;
              if(mul2==NULL){
                mul2=n;
                temp=n;
              }
              else{
                  temp->next=n;
                  temp=n;
              }
            }
        cur1=cur1->next;
      }
      if(carry){
          n=new node();
          n->data=carry;
          temp->next=n;
          temp=n;
         
      }
      temp->next=NULL;
      
      mul1=add(mul1,mul2,k);
      k++;
      temp2=temp2->next;
  }
  mul1=reverse(mul1);
  long long prod=0;
  const long long MAX=1000000007;
  while(mul1){
      prod=prod*10+mul1->data;
      if(prod>=MAX){
          prod=prod%MAX;
      }
      mul1=mul1->next;
  }
  if(prod>=MAX) prod=prod%MAX;
  return prod;
}

 

answered Oct 15, 2016 by Vivek Mangal 2
0 votes

suppose the result would be too big to be respresented by int or int64_t

both input and output are represented by linked list

#include <iostream>
using namespace std;
struct node
{
  int data;
  node* next;
  node():data(0),next(nullptr){}
  node(int data_, node* next_):data(data_),next(next_){}
};

/* Multiply contents of two linked lists */
node* multiplyTwoLists (node* first, node* second)
{
  if(!first||!second)return nullptr;
  node* res=new node;
  node* c1=first;
  node* s1=res;
  while(c1){
    node* s2=s1;
    node* c2=second;
    while(c2){
      int tmp=c1->data*c2->data;
      node* c3=s2;
      while(tmp){
        c3->data+=tmp%10;
        tmp=tmp/10;
        tmp+=c3->data/10;
        c3->data=c3->data%10;
        if(!c3->next)c3->next=new node;
        c3=c3->next;
      }
      c2=c2->next;
      if(!s2->next)s2->next=new node;
      s2=s2->next;
    }
    c1=c1->next;
    if(!s1->next)s1->next=new node;
    s1=s1->next;
  }
  return res;
}


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

int main() {
  node* first=new node(3, nullptr);
  first =new node(2,first);
  first =new node(4,first);
  node* second=new node(4, nullptr);
  second =new node(5,second);
  second =new node(6,second);
  second =new node(7,second);
  printList(multiplyTwoLists(first,second));
  cout << 324*4567 << endl;
  return 0;
}
answered Nov 29, 2016 by ZuoWang

Please log in or register to answer this question.

...