This is from the archives, when I’d take more notes. Let me take those insights and preserve them here.
Despite the name, this question is really about linked lists. I’ll assume you’ve read the question and understood it. Now, a few observations:
- We get to iterate through the list in the “nice” way: we can start adding digits immediately.
- Built into the question is the problem of “carries” — we’re adding things, but each node can only hold the values 0-9, and so if there’s overflow we have to carry that.
- This is a linked-list-merge-style question (see featured post here), so we should be mindful of the “end” of each of our two linked lists and how we should handle them. A moment’s thought also suggests that the carry value can also play a role here!
- No negative numbers! The natural approach doesn’t suggest we’d introduce an erroneous leading zero or anything either. So it doesn’t seem there’s anything too tricky here.
Some insights:
- We can treat the carry value as a “virtual” node: this tightens up our iterations and puts everything into one loop.
- The approach I took was to create a new, third list. Another option (in practice; I assume the autograder would accept this too) would be to make this transformation destructive of one or both lists.
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* lastDigit = nullptr;
ListNode* result = nullptr;
bool carry = false;
while (l1 || l2 || carry) {
auto l1val = l1 ? l1->val : 0;
auto l2val = l2 ? l2->val : 0;
auto nextVal = l1val + l2val;
if (carry) {
nextVal++;
carry = false;
}
if (nextVal > 9) {
nextVal -= 10;
carry = true;
// assert(nextVal < 10 && nextVal >= 0)
}
if (!result) {
result = new ListNode(nextVal);
lastDigit = result;
} else {
lastDigit->next = new ListNode(nextVal);
lastDigit = lastDigit->next;
}
if (l1) { l1 = l1->next; }
if (l2) { l2 = l2->next; }
}
return result;
}
};
I think this is a nice interview question.