Microsoft Interview Question
Software Engineer in TestsNot that easy - incase of carry u need to handle stuff like s->data + s2-> data - the sum has to be checked for carry - so do a %10 and a /10 and add the values accordingly
for the second case length of both link lists need not to be equal. so we have to start from last node and add and keep track of cary.. in case of singly link list this can be done recursively
static int productListImp(List *p, int *val);
int productList(List *p)
{
int val = 0;
if(p == NULL)
return -1;
return productListImp(p, &val);
}
static int productListImp(List *p, int *val)
{
int product;
if(p->next == NULL)
return p->data;
product = productListImp(p->next);
(*val)++;
product = (p->data) * (pow(10, *val) + product;
return product
}
struct Node {
- Alok August 02, 2011int data;
struct Node *next;
};
int get_int(struct Node *s)
{
int sum=0;
while(s){
sum=sum*10+(s->data);
s=s->next;
}
return sum;
}