Given a linked list such that each node is a digit from a number.
For example: If number is 1234, the corresponding linked list is:
1 -> 2 -> 3 -> 4 -> [\]
The problem is to add 1 to this number. So the output will be:
1 -> 2 -> 3 -> 5 -> [\]
We can do it recursively in O(n) time with O(n) space. Is it possible to optimize the solution to O(logn) time complexity?
Update:
n is total number of digits.
No, provided you have no additional data structure.
Since the last node in the list will always change, and it takes O(n) time for any algorithm to reach there (you must chase the whole list if you have no additional data structure), that gives O(n) as your lower bound.