Sum of two lists issue
Closed this issue · 2 comments
Attempting to test with below list pairs:
[9, 9, 9, 8, 9]
[1, 1, 1, 1, 1]
will most probably generate a resultant list of [0, 1, 1, 0, 1, 1] --> 011011
which violates the idea of summation.
You could try to reverse the list with O(n)
instead and then sum the values with the traditional way (head --> tail
but due to reverse this would be actually old tail --> old head
).
Hello, apologies for the confusion but the question indicated that for this scenario, the head of the linked list (both the inputs and output) would point to the least significant digit and so on. 1 -> 4 is actually 41.
Going by your example, 9 -> 9 -> 9 -> 8 -> 9 is actually the number 98999.
Similarly, 1 -> 1-> 1-> 1-> 1 is 11111.
Output is: 0 -> 1 -> 1 -> 0 -> 1 -> 1 which is actually 110110.
This satisfies the desired output where they REQUIRE the least significant digit to appear first. In a case where you want to output the digits as they appear (starting with the most significant digit), reversing the list would make sense. Thanks for pointing it out. I will add a comment at the top so people don't get confused.
Sorry about it, you are totally right under this assumption (reverse lists as input/output). Mea culpa ✋