Unofficial C Solution to Problem 2.5 in Cracking the Coding Interview (5th Edition)

9 May

The official solution for the backward problem is quite good. In contrast, there are two improvable places in the official solution for the forward problem (the follow up question).

On one hand, in the official implement, the list is double linked. So we could travel the list from end to begin, and solve the problem in the same way as the backward problem.

On the other hand, the padding operations are unnecessary. For all padded nodes, the values are all zero. So without padding operations, when we add the digits, we could use zero as default if the index is out of the list’s range.

The C code for backward list is as following:

The C code for the forward problem is as following:

Leave a Reply

Your email address will not be published. Required fields are marked *