Re: [algogeeks] Re: Addition Of numbers in SLL
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 (gap0) { 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.comalgogeeks%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.comalgogeeks%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.
Re: [algogeeks] Re: Addition Of numbers in SLL
@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. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Addition Of numbers in SLL
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 (gap0) { 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.comalgogeeks%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.
Re: [algogeeks] Re: Addition Of numbers in SLL
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 gogi.no...@gmail.com wrote: 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 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.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Rahul singhal RnD Engineer Tejas Networks Mobile- 09916969422 -- 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.
Re: [algogeeks] Re: Addition Of numbers in SLL
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 group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: Addition Of numbers in SLL
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 lokesh...@gmail.comwrote: 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 group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Rahul singhal RnD Engineer Tejas Networks Mobile- 09916969422 -- 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.
Re: [algogeeks] Re: Addition Of numbers in SLL
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 nitk.ra...@gmail.com 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 we dont need to reverse again.Hope,i made myself clear. On Sat, Aug 14, 2010 at 11:40 PM, Lokesh Agarwal lokesh...@gmail.comwrote: 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 group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en. -- Rahul singhal RnD Engineer Tejas Networks Mobile- 09916969422 -- 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. -- 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.
Re: [algogeeks] Re: Addition Of numbers in SLL
i think we can use recursion method to reverse the list -- Prashant Kulkarni On Sat, Aug 14, 2010 at 11:40 PM, Lokesh Agarwal lokesh...@gmail.comwrote: 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 group, send email to algogeeks+unsubscr...@googlegroups.comalgogeeks%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.