You are given two non-empty linked lists representing two non-negative
integers. The digits are stored in reverse order and each of their
nodes contain a single digit. Add the two numbers and return it as a
linked list.

You may assume the two numbers do not contain any leading zero, except
the number 0 itself.

Example:

Input: (2,4,3) + (5,6,4)

Output: 7,0,8

Explanation: 342 + 465 = 807

I did a coding problem and I am wondering what I could have done better or done differently.

I'd suggest that you write down the logic in plain English before turning it into code.

To add two lists:
* Add the first digits
* If the sum is less than ten:
* Store the sum
* Add the rest of the lists
else
* Subtract ten
* Store the sum (after subtraction)
* Add the rest of the lists with the carried ten
Keep going until you've run out digits in both lists.

With a written plan in front of you, you'll find it easier to structure your code clearly and keep it short.

Think carefully about the "contract" of your function: what exactly should the output of addTwoNumbers look like? In your code, the line if(l3->next->val>9) is a red flag: it should not be possible to store numbers bigger than 9 in a node!

Now a couple of sneaky tricks to make it easier:

Remember those boolean circuit design classes where you learned about a binary "half adder" and then a "full adder"? So you have an "add" function and an "add with carry" function. But C++ allows default arguments, so you can roll them into a single function (if the carry isn't specified, it defaults to zero).

Instead of having "if this is null" checks in multiple places in your function, you can put that logic into a helper function, and have it treat nulls as zeros.

(Note: I don't use C++ professionally--I'm writing more from the perspective of algorithm design, not language features, and Deduplicator warns us in the comments that pointers are tricky!--if you wanted to use this in a production system, there's a bit more work to be done.)

\$\begingroup\$@Deduplicator, it's not obvious to me what you're referring to in your comment. Would you mind giving a little more detail?\$\endgroup\$
– Alexander HanyszMar 31 '18 at 12:50

\$\begingroup\$Well, what happens if one of the allocations fails, maybe not the first? What happens if one number is longer than the other? Also, do you know that calling a member-function using a null-pointer is UB, so this != nullptr always holds?\$\endgroup\$
– DeduplicatorMar 31 '18 at 13:07

\$\begingroup\$Thanks for the feedback re this!=nullptr. It worked on my platform, and I didn't realise it wasn't portable. What happens if one number is longer than the other? Answer: I tested it and there's no problem here. What if the allocations fail? Yes, that's bad news. I'm just giving some short code to illustrate a point about algorithm design, not trying to write robust enterprise software here. The OP didn't ask about error handling, and the other answers didn't address it either.\$\endgroup\$
– Alexander HanyszApr 1 '18 at 0:41

\$\begingroup\$Edited to avoid accessing member functions through null pointers -- I've moved these checks into helper functions. Hopefully no undefined behaviour now. @Deduplicator, do you mind taking another look at it?\$\endgroup\$
– Alexander HanyszApr 2 '18 at 5:15