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
If the end of both addends is reached simultaneously
If carry = 1
Create a linked list node for the sum
Store 1 in the node
If the end of one list is reached before the end of the other,
consider that addend as a stream of zeros as long as the remainder of
the other addend; propogate the carry until carry = 0 and copy the
remainder of the addend.
Dave
On Aug 14, 2:22 am, AlgoBoy <[email protected]> wrote:
> Add two numbers represented in a SLL. Each digit is represented as a
> node...the length of the lists may be more than 2000...
> Wat is the most efficient soln...store the added digits in another
> SLL...and return the head as the answer
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.