Re: [algogeeks] Re: Addition Of numbers in SLL

2010-08-17 Thread Algoose chase
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) fo

Re: [algogeeks] Re: Addition Of numbers in SLL

2010-08-15 Thread Ashish Goel
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(

[algogeeks] Re: Addition Of numbers in SLL

2010-08-15 Thread Dave
@Manjunath: A small snippet of my explanation of what? What do you not understand? Dave On Aug 15, 1:49 am, Manjunath Manohar 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" g

Re: [algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Manjunath Manohar
@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..

[algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Dave
Don't reverse the list. Just store it from low to high order digits. I.e., the head points to the ones digit, which points to the tens digit, etc. That is the assumption I made with my algorithm presented in the second post of this thread, because almost all operations use the number beginning with

Re: [algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Prashant Kulkarni
i think we can use recursion method to reverse the list -- Prashant Kulkarni On Sat, Aug 14, 2010 at 11:40 PM, Lokesh Agarwal wrote: > how can you traverse from last without reversing it. > > and there is no need fof using extra stack space. > > -- > You received this message because you are

Re: [algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Manjunath Manohar
Reversing and then again reversing the answer will not be an efficient algorithm... on the fly computation of sum must be done...any ideas On 8/14/10, Rahul Singhal wrote: > I men to say ki just traverse from last instead of reversing it and storing > result in a stack in linked list form so that

Re: [algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Rahul Singhal
I men to say ki just traverse from last instead of reversing it and storing result in a stack in linked list form so that we dont need to reverse again.Hope,i made myself clear. On Sat, Aug 14, 2010 at 11:40 PM, Lokesh Agarwal wrote: > how can you traverse from last without reversing it. > > and

Re: [algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Lokesh Agarwal
how can you traverse from last without reversing it. and there is no need fof using extra stack space. -- 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

Re: [algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Rahul Singhal
i think we can access numbers from last so no need to reverse it and also we can store it in linke list in stack way so again no need to reverse the linked list. On Sat, Aug 14, 2010 at 7:03 PM, Gaurav Singh wrote: > Reversing the lists and then adding and then reversing the final list > is the

[algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Gaurav Singh
Reversing the lists and then adding and then reversing the final list is the most appropriate method. Bcoz the lists may contain arbitarily large numbers, so forming integers then adding is not logical here. -- You received this message because you are subscribed to the Google Groups "Algori

[algogeeks] Re: Addition Of numbers in SLL

2010-08-14 Thread Dave
Set carry = 0 Walk the linked lists of the addends simultaneoulsy At each pair of list nodes, add the digits and carry Create a linked list node for the sum If the sum is less than 10: Store the sum Set carry = 0 Otherwise Store sum - 10 Set carry = 1