c++ - Most optimal way to find the sum of 2 numbers represented as linked lists -
I was trying to write a program for the problem mentioned above, the number (ie lists) can be of uneven length
The complexity of this should be o (n + m). Is it anyway to reduce it, or is it better to do it?
The best thing I do is store the reverse In order, so 43,712 is stored in this form:
2 -> 1 - & gt; 7 - & gt; 3 - & gt; 4
This arithmetic operation makes very easy.
Displaying a number can be either more or more simpler with an repeated algorithm Note: It all assumes list of linked lists alone.
Edit: But you later said that you have no choice in storage format. As such, your best bet is to reverse the two lists, make extra and then reverse the results.
Comments
Post a Comment