The Solution is pretty straight forward when you long number is represented
in reverse order in linked list.
If the number is not in reverse order, We need an Explicit stack or we must
Use Recursion .

Other way around this is to construct another parallel linked list along
with Sum(linked list) for maintaining the carry information and use multiple
passes until the Carry linked list completely vanishes.
Whenever you find the sum digit , Use sum->digit = (list1->digit +
list2->digit + Carry->next->digit) % 10
                                                        Carry->digit =
(list1->digit + list2->digit + Carry->digit) / 10

On Mon, Aug 16, 2010 at 8:03 AM, Ashish Goel <ashg...@gmail.com> wrote:

>  int add(struct node* pL1, struct node * pl2,int gap/*no of digits in l1
> -no of digits in l2*/)
> { //assumption, # of nodes in L1 is > # of nodes in L2, make sure this
> before calling this, counting nodes is less costlier than reversal
>
>
> if (!(pl1) || !(pl2)) return 0;
>
> if (gap>0)
> {
>      carry = add(pL1->next, pL2, gap-1);
>      carry = (pl1->data+carry)/10;
>      pl1->data =(pl1->data+carry) %10;
>      return carry;
> }
> else
> {
>     carry = add(pL1->next, pl2->next, gap -1);
>      carry = (pl1->data+pl2->data+carry)/10;
>      pl1->data =(pl1->data+pl2->data+carry) %10;
>      return carry;
> }
>
> }
>
> Best Regards
> Ashish Goel
> "Think positive and find fuel in failure"
> +919985813081
> +919966006652
>
>
> On Sun, Aug 15, 2010 at 12:19 PM, Manjunath Manohar <
> manjunath.n...@gmail.com> wrote:
>
>> @Dave..Can u provide a small snippet for ur explanation pls..
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algoge...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algoge...@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com<algogeeks%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to