The solution is obvious that just to go through the two linked lists at the same time, add the number and make the sum a new node into the result. However, this can be a very buggy program if not careful.
This time, we will try to find corner cases using test-driven development. So let’s create tests first.
When creating tests, we must come up all possible situations that may break the code. In industry production, we must also be careful of invalid or meaningless or harmful inputs (e.g. SQL injection), but we don’t have to worry about them here.
Before creating tests, let’s take another look at the input requirement: each input is a linked list of integers. During the interview, it is better to ask the interviewer if we can assume the input is valid, which in this case, yes, so we can assume that each linked list node is an integer from 0 to 9. When creating tests cases, we can hand calculate the expected outputs as well unless the algorithm is extremely complex.
Some tests and expected outputs can be: