title: Sum two Linked Lists
body: <p>I have a node like this:</p>
  <pre><code>struct ListNode {
      int val;
      ListNode *next;
      ListNode() 
         : val(0), next(nullptr) {}
      ListNode(int x) 
         : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) 
         : val(x), next(next) {}
  };
  </code></pre>
  <p>And I want to sum two link lists using recursion. I am not sure, but I guess it is tail recursion.</p>
  <p><strong>Input:</strong> l1 = [2,4,3], l2 = [5,6,4]</p>
  <p><strong>Output:</strong> [7,0,8]</p>
  <p><strong>Explanation:</strong> 342 + 465 = 807.</p>
  <p>Here is my code:</p>
  <pre><code>ListNode* addTwoNumbers(ListNode* l1, ListNode* l2, int carry = 0,ListNode *result = nullptr) {
      int n1 = l1 != nullptr ? l1-&gt;val : 0;
      int n2 = l2 != nullptr ? l2-&gt;val : 0;
      int val = (n1 + n2 + carry) % 10;
      if(result == nullptr){
          result = new ListNode(val);
      }else{
          result-&gt;next = new ListNode(val);
          result = result-&gt;next;
      }
      result = addTwoNumbers(l1-&gt;next != nullptr ? l1-&gt;next : nullptr,
                             l2-&gt;next != nullptr ? l2-&gt;next : nullptr, 
                             (n1 + n2 + carry) - 10,result);
      return result;
  }
  </code></pre>
  <p>But I am getting a segmentation fault, even when passing two single-node lists:</p>
  <pre><code>ListNode * a = new ListNode(1);
  ListNode * b = new ListNode(2);
  ListNode * c = addTwoNumbers(a, b); 
  std::cout &lt;&lt; c-&gt;val &lt;&lt; &quot;\n&quot;;
  </code></pre>
  <p>I cannot see what is wrong, it should work. What am I missing?</p>