Amazon Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
// l1, l2 be the linked lists
stack *s;
void main() {
reverse(l1) ;
reverse (l2);
s = (stack*) malloc(sizeof(stack));
add (l1, l2);
while(!isEmpty(s))
print pop();
}
void add (stack *l1, stack *l2, int carry) {
if(l1 == null && l2 == null) return;
if(l1 == null) a1 = 0;
else if(l2 == null) a2 = 0;
if(!l1)
a1 = l1->val;
if(!l2)
a2 = l2->val;
a3 = a1 + a2 + carry;
push(a3 % 10);
if(!l1 && !l2)
recurse(l1->next, l2->next, a3 / 10)
else if(!l1)
recurse(l1, l2->next, a3 / 10)
else if(!l2)
recurse(l1->next, l2, a3 / 10)
}
Dude... could you be a bit more clear with your question.
- Anonymous September 12, 2011"Add two numbers in a linked list using a stack?"
Why would I require stack or recursion to add 'two' numbers?!