# 12 line java solution

• /**

• public class ListNode {

• ``````int val;
``````
• ``````ListNode next;
``````
• ``````ListNode(int x) {
``````
• ``````    val = x;
``````
• ``````    next = null;
``````
• ``````}
``````
• }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
}

public ListNode addTwoNumbersWithCarryOver(ListNode l1,ListNode l2, int carryOver){
if (l1 == null) {
return carryOver == 0 ? l2 : addTwoNumbersWithCarryOver(new ListNode(carryOver), l2,0);
}

`````` if (l2 == null) {
return carryOver == 0 ? l1 : addTwoNumbersWithCarryOver(l1, new ListNode(carryOver),0);
}

int sumVal = l1.val + l2.val + carryOver;
ListNode returnVal = new ListNode(sumVal%10);
return returnVal;
``````

}
}

• it maybe short, but it takes too much space I think

• I am so stupid. I always admire those who can use recursion...
can anyone tell me how to learn to use recursion?

• Don't be so disappointed. Try to read others's code and practice. You can do it, too.

• Just try to think an recursive way yourself you will improve the more you think about it,I was in the same conditions a while back :)

• I feel recursion is more intuitive than loop after read the book call "the little schemer".

• You could be more concise by replacing:

``````if (l2 == null) {
return carryOver == 0 ? l1 : addTwoNumbersWithCarryOver(l1, new ListNode(carryOver),0);
}
``````

with:

``````if (l2 == null) {