Sum numbers represented as linked lists

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












Problem Description:




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.



 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
ListNode *l3 = new ListNode(0);

if(l1->next!=NULL && l2->next!=NULL)

l3->next = addTwoNumbers(l1->next,l2->next);

if(l3->next->val>9)

ListNode *temp;
temp = l3;
while(temp->next->val>9)

temp->next->next->val+=1;
temp->next->val-=10;
l3->val = l1->val+l2->val;
temp = temp->next;

return l3;


else
l3->val = l1->val + l2->val;

if(l3->val>9)

ListNode *tempB;
tempB = l3;
while(tempB->val>9)

if(tempB->next != NULL)

tempB->next->val+=1;
tempB->val-=10;
tempB = tempB->next;

else

ListNode *H = new ListNode(1);
tempB->next = H;
tempB->val -= 10;
tempB = tempB->next;



return l3;



return l3;




else if(l1->next == NULL && l2->next != NULL)

ListNode *temp = new ListNode(0);
l3->next = addTwoNumbers(temp,l2->next);
l3->val = l1->val + l2->val;
if(l3->val>9)

ListNode *tempB;
tempB = l3;
while(tempB->val>9)

if(tempB->next != NULL)

tempB->next->val+=1;
tempB->val-=10;
tempB = tempB->next;

else

ListNode *H = new ListNode(1);
tempB->next = H;
tempB->val -= 10;
tempB = tempB->next;



return l3;

else
return l3;


else if(l2->next == NULL && l1->next != NULL)

ListNode *temp = new ListNode(0);
l3->next = addTwoNumbers(l1->next,temp);
l3->val = l1->val + l2->val;
if(l3->val>9)

ListNode *tempB;
tempB = l3;
while(tempB->val>9)

if(tempB->next != NULL)

tempB->next->val+=1;
tempB->val-=10;
tempB = tempB->next;

else

ListNode *H = new ListNode(1);
tempB->next = H;
tempB->val -= 10;
tempB = tempB->next;



return l3;

else
return l3;


else
l3->val = l1->val + l2->val;
if(l3->val>9)

ListNode* l4 = new ListNode(1);
l3->val -=10;
l3->next = l4;
return l3;

else
return l3;










share|improve this question



























    up vote
    4
    down vote

    favorite












    Problem Description:




    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.



     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
    ListNode *l3 = new ListNode(0);

    if(l1->next!=NULL && l2->next!=NULL)

    l3->next = addTwoNumbers(l1->next,l2->next);

    if(l3->next->val>9)

    ListNode *temp;
    temp = l3;
    while(temp->next->val>9)

    temp->next->next->val+=1;
    temp->next->val-=10;
    l3->val = l1->val+l2->val;
    temp = temp->next;

    return l3;


    else
    l3->val = l1->val + l2->val;

    if(l3->val>9)

    ListNode *tempB;
    tempB = l3;
    while(tempB->val>9)

    if(tempB->next != NULL)

    tempB->next->val+=1;
    tempB->val-=10;
    tempB = tempB->next;

    else

    ListNode *H = new ListNode(1);
    tempB->next = H;
    tempB->val -= 10;
    tempB = tempB->next;



    return l3;



    return l3;




    else if(l1->next == NULL && l2->next != NULL)

    ListNode *temp = new ListNode(0);
    l3->next = addTwoNumbers(temp,l2->next);
    l3->val = l1->val + l2->val;
    if(l3->val>9)

    ListNode *tempB;
    tempB = l3;
    while(tempB->val>9)

    if(tempB->next != NULL)

    tempB->next->val+=1;
    tempB->val-=10;
    tempB = tempB->next;

    else

    ListNode *H = new ListNode(1);
    tempB->next = H;
    tempB->val -= 10;
    tempB = tempB->next;



    return l3;

    else
    return l3;


    else if(l2->next == NULL && l1->next != NULL)

    ListNode *temp = new ListNode(0);
    l3->next = addTwoNumbers(l1->next,temp);
    l3->val = l1->val + l2->val;
    if(l3->val>9)

    ListNode *tempB;
    tempB = l3;
    while(tempB->val>9)

    if(tempB->next != NULL)

    tempB->next->val+=1;
    tempB->val-=10;
    tempB = tempB->next;

    else

    ListNode *H = new ListNode(1);
    tempB->next = H;
    tempB->val -= 10;
    tempB = tempB->next;



    return l3;

    else
    return l3;


    else
    l3->val = l1->val + l2->val;
    if(l3->val>9)

    ListNode* l4 = new ListNode(1);
    l3->val -=10;
    l3->next = l4;
    return l3;

    else
    return l3;










    share|improve this question























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      Problem Description:




      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.



       ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
      ListNode *l3 = new ListNode(0);

      if(l1->next!=NULL && l2->next!=NULL)

      l3->next = addTwoNumbers(l1->next,l2->next);

      if(l3->next->val>9)

      ListNode *temp;
      temp = l3;
      while(temp->next->val>9)

      temp->next->next->val+=1;
      temp->next->val-=10;
      l3->val = l1->val+l2->val;
      temp = temp->next;

      return l3;


      else
      l3->val = l1->val + l2->val;

      if(l3->val>9)

      ListNode *tempB;
      tempB = l3;
      while(tempB->val>9)

      if(tempB->next != NULL)

      tempB->next->val+=1;
      tempB->val-=10;
      tempB = tempB->next;

      else

      ListNode *H = new ListNode(1);
      tempB->next = H;
      tempB->val -= 10;
      tempB = tempB->next;



      return l3;



      return l3;




      else if(l1->next == NULL && l2->next != NULL)

      ListNode *temp = new ListNode(0);
      l3->next = addTwoNumbers(temp,l2->next);
      l3->val = l1->val + l2->val;
      if(l3->val>9)

      ListNode *tempB;
      tempB = l3;
      while(tempB->val>9)

      if(tempB->next != NULL)

      tempB->next->val+=1;
      tempB->val-=10;
      tempB = tempB->next;

      else

      ListNode *H = new ListNode(1);
      tempB->next = H;
      tempB->val -= 10;
      tempB = tempB->next;



      return l3;

      else
      return l3;


      else if(l2->next == NULL && l1->next != NULL)

      ListNode *temp = new ListNode(0);
      l3->next = addTwoNumbers(l1->next,temp);
      l3->val = l1->val + l2->val;
      if(l3->val>9)

      ListNode *tempB;
      tempB = l3;
      while(tempB->val>9)

      if(tempB->next != NULL)

      tempB->next->val+=1;
      tempB->val-=10;
      tempB = tempB->next;

      else

      ListNode *H = new ListNode(1);
      tempB->next = H;
      tempB->val -= 10;
      tempB = tempB->next;



      return l3;

      else
      return l3;


      else
      l3->val = l1->val + l2->val;
      if(l3->val>9)

      ListNode* l4 = new ListNode(1);
      l3->val -=10;
      l3->next = l4;
      return l3;

      else
      return l3;










      share|improve this question













      Problem Description:




      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.



       ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
      ListNode *l3 = new ListNode(0);

      if(l1->next!=NULL && l2->next!=NULL)

      l3->next = addTwoNumbers(l1->next,l2->next);

      if(l3->next->val>9)

      ListNode *temp;
      temp = l3;
      while(temp->next->val>9)

      temp->next->next->val+=1;
      temp->next->val-=10;
      l3->val = l1->val+l2->val;
      temp = temp->next;

      return l3;


      else
      l3->val = l1->val + l2->val;

      if(l3->val>9)

      ListNode *tempB;
      tempB = l3;
      while(tempB->val>9)

      if(tempB->next != NULL)

      tempB->next->val+=1;
      tempB->val-=10;
      tempB = tempB->next;

      else

      ListNode *H = new ListNode(1);
      tempB->next = H;
      tempB->val -= 10;
      tempB = tempB->next;



      return l3;



      return l3;




      else if(l1->next == NULL && l2->next != NULL)

      ListNode *temp = new ListNode(0);
      l3->next = addTwoNumbers(temp,l2->next);
      l3->val = l1->val + l2->val;
      if(l3->val>9)

      ListNode *tempB;
      tempB = l3;
      while(tempB->val>9)

      if(tempB->next != NULL)

      tempB->next->val+=1;
      tempB->val-=10;
      tempB = tempB->next;

      else

      ListNode *H = new ListNode(1);
      tempB->next = H;
      tempB->val -= 10;
      tempB = tempB->next;



      return l3;

      else
      return l3;


      else if(l2->next == NULL && l1->next != NULL)

      ListNode *temp = new ListNode(0);
      l3->next = addTwoNumbers(l1->next,temp);
      l3->val = l1->val + l2->val;
      if(l3->val>9)

      ListNode *tempB;
      tempB = l3;
      while(tempB->val>9)

      if(tempB->next != NULL)

      tempB->next->val+=1;
      tempB->val-=10;
      tempB = tempB->next;

      else

      ListNode *H = new ListNode(1);
      tempB->next = H;
      tempB->val -= 10;
      tempB = tempB->next;



      return l3;

      else
      return l3;


      else
      l3->val = l1->val + l2->val;
      if(l3->val>9)

      ListNode* l4 = new ListNode(1);
      l3->val -=10;
      l3->next = l4;
      return l3;

      else
      return l3;












      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 1 at 2:23









      200_success

      123k14142399




      123k14142399









      asked Mar 25 at 18:16









      naauao

      211




      211




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          10
          down vote













          Well, your code treats just about everything as a special-case. That not only lacks elegance, it's very verbose, error-prone and inefficient.



          1. First, the problem is symmetrical in the two arguments. So, you can swap the arguments without changing anything.

          2. You are only reading the passed lists, never modifying them. So, const seems appropriate.

          3. If an allocation throws, you simply leak all already-allocated nodes. That's generally inacceptible.

          4. Consider writing a proper list-class instead of throwing around loose nodes.

          5. Your problem is better solved with iteration than recursion, as C++ does not do GC nor guarantees tail-recursion.

          So, let's look at it again, but use iteration. Still, I won't write the list-abstraction, as that would be a major part in itself:



           ListNode* addTwoNumbers(const ListNode* a, const ListNode* b) 
          ListNode dummy(0);
          auto p = &dummy;
          int carry = 0;

          try
          while (a && b)
          carry += a->val + b->val;
          p = p->next = new ListNode(carry % 10);
          carry /= 10;
          a = a->next;
          b = b->next;


          if(b)
          a = b;
          while (a)
          carry += a->val;
          p = p->next = new ListNode(carry % 10);
          carry /= 10;
          a = a->next;


          if (carry)
          p->next = new ListNode(carry);

          return dummy->next;

          catch (...)
          p = dummy->next;
          while (p)
          auto x = p->next;
          delete p;
          p = x;

          throw;







          share|improve this answer






























            up vote
            4
            down vote













            First: split it up in multiple methods.



            Second: You can resuse parts of your code: f(l1->next == NULL && l2->next != NULL) and if(l2->next == NULL && l1->next != NULL) will be identical if l1and l2 are interchanged.



            Why do ypu initialise ListNode with 0 or 1 and later reassign the value with the sum of digits? This can be done at once.



            When only one list still contain digits the handling can be simplified by specialisation.



            Personally I dislike variables like l1, l2, l3 especially as l and 1 can be hard to distinguish.



            With the above mentioned I tried to restructure your code (without testing for Bugs introduced by me) to clarify my proposals.



            struct ListNode 
            ListNode* next;
            int val;

            ListNode(int arg) next = nullptr; val = arg;
            ;

            ListNode* addTwoNumbers(ListNode* node1, ListNode* node2)

            if (node1->next != nullptr && node2->next != nullptr)

            return addTwoNodes(node1, node2);


            else if (node1->next == nullptr && node2->next != nullptr)

            return addOneNode(node1->val, node2);


            else if (node2->next == nullptr && node1->next != nullptr)

            return addOneNode(node2->val, node1);


            else
            return addTwoDigits(node1->val, node2->val);



            ListNode* addTwoNodes(ListNode* node1, ListNode* node2)

            ListNode *result = new ListNode(node1->val + node2->val);
            if (result->val > 9)

            result->val -= 10;
            ListNode tmp(1);
            if (node1->next != nullptr)

            ListNode const * const next = node1->next;
            tmp.val += next->val;
            tmp.next = next->next;

            result->next = addTwoNumbers(&tmp, node2->next);

            else

            result->next = addTwoNumbers(node1->next, node2->next);

            return result;


            ListNode* addOneNode(int val, ListNode* node)

            ListNode *result = new ListNode(val + node->val);
            if (result->val > 9)

            result->val -= 10;
            result->next = addOneNode(1, node->next);

            else

            ListNode *tmp = result;
            for (ListNode *arg = node->next; arg != nullptr; arg = arg->next)

            result->next = new ListNode(arg->val);


            return result;


            ListNode* addTwoDigits(int val1, int val2)

            ListNode *result = new ListNode(val1 + val2);

            if (result->val > 9)

            result->val -= 10;
            result->next = new ListNode(1);

            return result;






            share|improve this answer




























              up vote
              1
              down vote













              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.)



              Putting it all together, I get this version:



              #include <iostream>
              using namespace std;

              struct Digits
              Digits* next_node;
              int value;

              Digits(int arg)
              if (arg < 10) value = arg; next_node = nullptr;
              else value = arg % 10; next_node = new Digits(arg/10);

              ;

              int valueOrZero(Digits* node) return node==nullptr ? 0 : node->value;

              Digits* nextOrNull(Digits* node) return node==nullptr ? nullptr : node->next_node;

              string digitsToString(Digits* node)
              return node==nullptr ? "" : digitsToString(node->next_node) + to_string(node->value);



              Digits* addTwoNumbers(Digits* node1, Digits* node2, int carry=0)

              if ((node1==nullptr) & (node2==nullptr))
              return(new Digits(carry));

              else
              int tens=0;
              int ones = valueOrZero(node1) + valueOrZero(node2) + carry;
              if (ones >= 10)
              tens = ones/10;
              ones = ones % 10;

              Digits* answer = new Digits(ones);
              answer->next_node = addTwoNumbers(nextOrNull(node1), nextOrNull(node2), tens);
              return answer;




              void testAdd(int val1, int val2)
              Digits *num1 = new Digits(val1);
              Digits *num2 = new Digits(val2);
              Digits *num3 = addTwoNumbers(num1, num2);
              cout << digitsToString(num1) << "+" << digitsToString(num2) << " = " << digitsToString(num3) << endl;



              int main(int argc, char* argv)
              testAdd(1,1);
              testAdd(8,9);
              testAdd(23,8);
              testAdd(999,25);






              share|improve this answer























              • The obvious flaws are error-handling and different length numbers.
                – Deduplicator
                Mar 30 at 13:24










              • @Deduplicator, it's not obvious to me what you're referring to in your comment. Would you mind giving a little more detail?
                – Alexander Hanysz
                Mar 31 at 12:50










              • 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?
                – Deduplicator
                Mar 31 at 13:07










              • 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.
                – Alexander Hanysz
                Apr 1 at 0:41










              • 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?
                – Alexander Hanysz
                Apr 2 at 5:15










              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190451%2fsum-numbers-represented-as-linked-lists%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              10
              down vote













              Well, your code treats just about everything as a special-case. That not only lacks elegance, it's very verbose, error-prone and inefficient.



              1. First, the problem is symmetrical in the two arguments. So, you can swap the arguments without changing anything.

              2. You are only reading the passed lists, never modifying them. So, const seems appropriate.

              3. If an allocation throws, you simply leak all already-allocated nodes. That's generally inacceptible.

              4. Consider writing a proper list-class instead of throwing around loose nodes.

              5. Your problem is better solved with iteration than recursion, as C++ does not do GC nor guarantees tail-recursion.

              So, let's look at it again, but use iteration. Still, I won't write the list-abstraction, as that would be a major part in itself:



               ListNode* addTwoNumbers(const ListNode* a, const ListNode* b) 
              ListNode dummy(0);
              auto p = &dummy;
              int carry = 0;

              try
              while (a && b)
              carry += a->val + b->val;
              p = p->next = new ListNode(carry % 10);
              carry /= 10;
              a = a->next;
              b = b->next;


              if(b)
              a = b;
              while (a)
              carry += a->val;
              p = p->next = new ListNode(carry % 10);
              carry /= 10;
              a = a->next;


              if (carry)
              p->next = new ListNode(carry);

              return dummy->next;

              catch (...)
              p = dummy->next;
              while (p)
              auto x = p->next;
              delete p;
              p = x;

              throw;







              share|improve this answer



























                up vote
                10
                down vote













                Well, your code treats just about everything as a special-case. That not only lacks elegance, it's very verbose, error-prone and inefficient.



                1. First, the problem is symmetrical in the two arguments. So, you can swap the arguments without changing anything.

                2. You are only reading the passed lists, never modifying them. So, const seems appropriate.

                3. If an allocation throws, you simply leak all already-allocated nodes. That's generally inacceptible.

                4. Consider writing a proper list-class instead of throwing around loose nodes.

                5. Your problem is better solved with iteration than recursion, as C++ does not do GC nor guarantees tail-recursion.

                So, let's look at it again, but use iteration. Still, I won't write the list-abstraction, as that would be a major part in itself:



                 ListNode* addTwoNumbers(const ListNode* a, const ListNode* b) 
                ListNode dummy(0);
                auto p = &dummy;
                int carry = 0;

                try
                while (a && b)
                carry += a->val + b->val;
                p = p->next = new ListNode(carry % 10);
                carry /= 10;
                a = a->next;
                b = b->next;


                if(b)
                a = b;
                while (a)
                carry += a->val;
                p = p->next = new ListNode(carry % 10);
                carry /= 10;
                a = a->next;


                if (carry)
                p->next = new ListNode(carry);

                return dummy->next;

                catch (...)
                p = dummy->next;
                while (p)
                auto x = p->next;
                delete p;
                p = x;

                throw;







                share|improve this answer

























                  up vote
                  10
                  down vote










                  up vote
                  10
                  down vote









                  Well, your code treats just about everything as a special-case. That not only lacks elegance, it's very verbose, error-prone and inefficient.



                  1. First, the problem is symmetrical in the two arguments. So, you can swap the arguments without changing anything.

                  2. You are only reading the passed lists, never modifying them. So, const seems appropriate.

                  3. If an allocation throws, you simply leak all already-allocated nodes. That's generally inacceptible.

                  4. Consider writing a proper list-class instead of throwing around loose nodes.

                  5. Your problem is better solved with iteration than recursion, as C++ does not do GC nor guarantees tail-recursion.

                  So, let's look at it again, but use iteration. Still, I won't write the list-abstraction, as that would be a major part in itself:



                   ListNode* addTwoNumbers(const ListNode* a, const ListNode* b) 
                  ListNode dummy(0);
                  auto p = &dummy;
                  int carry = 0;

                  try
                  while (a && b)
                  carry += a->val + b->val;
                  p = p->next = new ListNode(carry % 10);
                  carry /= 10;
                  a = a->next;
                  b = b->next;


                  if(b)
                  a = b;
                  while (a)
                  carry += a->val;
                  p = p->next = new ListNode(carry % 10);
                  carry /= 10;
                  a = a->next;


                  if (carry)
                  p->next = new ListNode(carry);

                  return dummy->next;

                  catch (...)
                  p = dummy->next;
                  while (p)
                  auto x = p->next;
                  delete p;
                  p = x;

                  throw;







                  share|improve this answer















                  Well, your code treats just about everything as a special-case. That not only lacks elegance, it's very verbose, error-prone and inefficient.



                  1. First, the problem is symmetrical in the two arguments. So, you can swap the arguments without changing anything.

                  2. You are only reading the passed lists, never modifying them. So, const seems appropriate.

                  3. If an allocation throws, you simply leak all already-allocated nodes. That's generally inacceptible.

                  4. Consider writing a proper list-class instead of throwing around loose nodes.

                  5. Your problem is better solved with iteration than recursion, as C++ does not do GC nor guarantees tail-recursion.

                  So, let's look at it again, but use iteration. Still, I won't write the list-abstraction, as that would be a major part in itself:



                   ListNode* addTwoNumbers(const ListNode* a, const ListNode* b) 
                  ListNode dummy(0);
                  auto p = &dummy;
                  int carry = 0;

                  try
                  while (a && b)
                  carry += a->val + b->val;
                  p = p->next = new ListNode(carry % 10);
                  carry /= 10;
                  a = a->next;
                  b = b->next;


                  if(b)
                  a = b;
                  while (a)
                  carry += a->val;
                  p = p->next = new ListNode(carry % 10);
                  carry /= 10;
                  a = a->next;


                  if (carry)
                  p->next = new ListNode(carry);

                  return dummy->next;

                  catch (...)
                  p = dummy->next;
                  while (p)
                  auto x = p->next;
                  delete p;
                  p = x;

                  throw;








                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Mar 26 at 15:06


























                  answered Mar 25 at 20:21









                  Deduplicator

                  9,8721844




                  9,8721844






















                      up vote
                      4
                      down vote













                      First: split it up in multiple methods.



                      Second: You can resuse parts of your code: f(l1->next == NULL && l2->next != NULL) and if(l2->next == NULL && l1->next != NULL) will be identical if l1and l2 are interchanged.



                      Why do ypu initialise ListNode with 0 or 1 and later reassign the value with the sum of digits? This can be done at once.



                      When only one list still contain digits the handling can be simplified by specialisation.



                      Personally I dislike variables like l1, l2, l3 especially as l and 1 can be hard to distinguish.



                      With the above mentioned I tried to restructure your code (without testing for Bugs introduced by me) to clarify my proposals.



                      struct ListNode 
                      ListNode* next;
                      int val;

                      ListNode(int arg) next = nullptr; val = arg;
                      ;

                      ListNode* addTwoNumbers(ListNode* node1, ListNode* node2)

                      if (node1->next != nullptr && node2->next != nullptr)

                      return addTwoNodes(node1, node2);


                      else if (node1->next == nullptr && node2->next != nullptr)

                      return addOneNode(node1->val, node2);


                      else if (node2->next == nullptr && node1->next != nullptr)

                      return addOneNode(node2->val, node1);


                      else
                      return addTwoDigits(node1->val, node2->val);



                      ListNode* addTwoNodes(ListNode* node1, ListNode* node2)

                      ListNode *result = new ListNode(node1->val + node2->val);
                      if (result->val > 9)

                      result->val -= 10;
                      ListNode tmp(1);
                      if (node1->next != nullptr)

                      ListNode const * const next = node1->next;
                      tmp.val += next->val;
                      tmp.next = next->next;

                      result->next = addTwoNumbers(&tmp, node2->next);

                      else

                      result->next = addTwoNumbers(node1->next, node2->next);

                      return result;


                      ListNode* addOneNode(int val, ListNode* node)

                      ListNode *result = new ListNode(val + node->val);
                      if (result->val > 9)

                      result->val -= 10;
                      result->next = addOneNode(1, node->next);

                      else

                      ListNode *tmp = result;
                      for (ListNode *arg = node->next; arg != nullptr; arg = arg->next)

                      result->next = new ListNode(arg->val);


                      return result;


                      ListNode* addTwoDigits(int val1, int val2)

                      ListNode *result = new ListNode(val1 + val2);

                      if (result->val > 9)

                      result->val -= 10;
                      result->next = new ListNode(1);

                      return result;






                      share|improve this answer

























                        up vote
                        4
                        down vote













                        First: split it up in multiple methods.



                        Second: You can resuse parts of your code: f(l1->next == NULL && l2->next != NULL) and if(l2->next == NULL && l1->next != NULL) will be identical if l1and l2 are interchanged.



                        Why do ypu initialise ListNode with 0 or 1 and later reassign the value with the sum of digits? This can be done at once.



                        When only one list still contain digits the handling can be simplified by specialisation.



                        Personally I dislike variables like l1, l2, l3 especially as l and 1 can be hard to distinguish.



                        With the above mentioned I tried to restructure your code (without testing for Bugs introduced by me) to clarify my proposals.



                        struct ListNode 
                        ListNode* next;
                        int val;

                        ListNode(int arg) next = nullptr; val = arg;
                        ;

                        ListNode* addTwoNumbers(ListNode* node1, ListNode* node2)

                        if (node1->next != nullptr && node2->next != nullptr)

                        return addTwoNodes(node1, node2);


                        else if (node1->next == nullptr && node2->next != nullptr)

                        return addOneNode(node1->val, node2);


                        else if (node2->next == nullptr && node1->next != nullptr)

                        return addOneNode(node2->val, node1);


                        else
                        return addTwoDigits(node1->val, node2->val);



                        ListNode* addTwoNodes(ListNode* node1, ListNode* node2)

                        ListNode *result = new ListNode(node1->val + node2->val);
                        if (result->val > 9)

                        result->val -= 10;
                        ListNode tmp(1);
                        if (node1->next != nullptr)

                        ListNode const * const next = node1->next;
                        tmp.val += next->val;
                        tmp.next = next->next;

                        result->next = addTwoNumbers(&tmp, node2->next);

                        else

                        result->next = addTwoNumbers(node1->next, node2->next);

                        return result;


                        ListNode* addOneNode(int val, ListNode* node)

                        ListNode *result = new ListNode(val + node->val);
                        if (result->val > 9)

                        result->val -= 10;
                        result->next = addOneNode(1, node->next);

                        else

                        ListNode *tmp = result;
                        for (ListNode *arg = node->next; arg != nullptr; arg = arg->next)

                        result->next = new ListNode(arg->val);


                        return result;


                        ListNode* addTwoDigits(int val1, int val2)

                        ListNode *result = new ListNode(val1 + val2);

                        if (result->val > 9)

                        result->val -= 10;
                        result->next = new ListNode(1);

                        return result;






                        share|improve this answer























                          up vote
                          4
                          down vote










                          up vote
                          4
                          down vote









                          First: split it up in multiple methods.



                          Second: You can resuse parts of your code: f(l1->next == NULL && l2->next != NULL) and if(l2->next == NULL && l1->next != NULL) will be identical if l1and l2 are interchanged.



                          Why do ypu initialise ListNode with 0 or 1 and later reassign the value with the sum of digits? This can be done at once.



                          When only one list still contain digits the handling can be simplified by specialisation.



                          Personally I dislike variables like l1, l2, l3 especially as l and 1 can be hard to distinguish.



                          With the above mentioned I tried to restructure your code (without testing for Bugs introduced by me) to clarify my proposals.



                          struct ListNode 
                          ListNode* next;
                          int val;

                          ListNode(int arg) next = nullptr; val = arg;
                          ;

                          ListNode* addTwoNumbers(ListNode* node1, ListNode* node2)

                          if (node1->next != nullptr && node2->next != nullptr)

                          return addTwoNodes(node1, node2);


                          else if (node1->next == nullptr && node2->next != nullptr)

                          return addOneNode(node1->val, node2);


                          else if (node2->next == nullptr && node1->next != nullptr)

                          return addOneNode(node2->val, node1);


                          else
                          return addTwoDigits(node1->val, node2->val);



                          ListNode* addTwoNodes(ListNode* node1, ListNode* node2)

                          ListNode *result = new ListNode(node1->val + node2->val);
                          if (result->val > 9)

                          result->val -= 10;
                          ListNode tmp(1);
                          if (node1->next != nullptr)

                          ListNode const * const next = node1->next;
                          tmp.val += next->val;
                          tmp.next = next->next;

                          result->next = addTwoNumbers(&tmp, node2->next);

                          else

                          result->next = addTwoNumbers(node1->next, node2->next);

                          return result;


                          ListNode* addOneNode(int val, ListNode* node)

                          ListNode *result = new ListNode(val + node->val);
                          if (result->val > 9)

                          result->val -= 10;
                          result->next = addOneNode(1, node->next);

                          else

                          ListNode *tmp = result;
                          for (ListNode *arg = node->next; arg != nullptr; arg = arg->next)

                          result->next = new ListNode(arg->val);


                          return result;


                          ListNode* addTwoDigits(int val1, int val2)

                          ListNode *result = new ListNode(val1 + val2);

                          if (result->val > 9)

                          result->val -= 10;
                          result->next = new ListNode(1);

                          return result;






                          share|improve this answer













                          First: split it up in multiple methods.



                          Second: You can resuse parts of your code: f(l1->next == NULL && l2->next != NULL) and if(l2->next == NULL && l1->next != NULL) will be identical if l1and l2 are interchanged.



                          Why do ypu initialise ListNode with 0 or 1 and later reassign the value with the sum of digits? This can be done at once.



                          When only one list still contain digits the handling can be simplified by specialisation.



                          Personally I dislike variables like l1, l2, l3 especially as l and 1 can be hard to distinguish.



                          With the above mentioned I tried to restructure your code (without testing for Bugs introduced by me) to clarify my proposals.



                          struct ListNode 
                          ListNode* next;
                          int val;

                          ListNode(int arg) next = nullptr; val = arg;
                          ;

                          ListNode* addTwoNumbers(ListNode* node1, ListNode* node2)

                          if (node1->next != nullptr && node2->next != nullptr)

                          return addTwoNodes(node1, node2);


                          else if (node1->next == nullptr && node2->next != nullptr)

                          return addOneNode(node1->val, node2);


                          else if (node2->next == nullptr && node1->next != nullptr)

                          return addOneNode(node2->val, node1);


                          else
                          return addTwoDigits(node1->val, node2->val);



                          ListNode* addTwoNodes(ListNode* node1, ListNode* node2)

                          ListNode *result = new ListNode(node1->val + node2->val);
                          if (result->val > 9)

                          result->val -= 10;
                          ListNode tmp(1);
                          if (node1->next != nullptr)

                          ListNode const * const next = node1->next;
                          tmp.val += next->val;
                          tmp.next = next->next;

                          result->next = addTwoNumbers(&tmp, node2->next);

                          else

                          result->next = addTwoNumbers(node1->next, node2->next);

                          return result;


                          ListNode* addOneNode(int val, ListNode* node)

                          ListNode *result = new ListNode(val + node->val);
                          if (result->val > 9)

                          result->val -= 10;
                          result->next = addOneNode(1, node->next);

                          else

                          ListNode *tmp = result;
                          for (ListNode *arg = node->next; arg != nullptr; arg = arg->next)

                          result->next = new ListNode(arg->val);


                          return result;


                          ListNode* addTwoDigits(int val1, int val2)

                          ListNode *result = new ListNode(val1 + val2);

                          if (result->val > 9)

                          result->val -= 10;
                          result->next = new ListNode(1);

                          return result;







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Mar 25 at 19:43









                          milbrandt

                          34715




                          34715




















                              up vote
                              1
                              down vote













                              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.)



                              Putting it all together, I get this version:



                              #include <iostream>
                              using namespace std;

                              struct Digits
                              Digits* next_node;
                              int value;

                              Digits(int arg)
                              if (arg < 10) value = arg; next_node = nullptr;
                              else value = arg % 10; next_node = new Digits(arg/10);

                              ;

                              int valueOrZero(Digits* node) return node==nullptr ? 0 : node->value;

                              Digits* nextOrNull(Digits* node) return node==nullptr ? nullptr : node->next_node;

                              string digitsToString(Digits* node)
                              return node==nullptr ? "" : digitsToString(node->next_node) + to_string(node->value);



                              Digits* addTwoNumbers(Digits* node1, Digits* node2, int carry=0)

                              if ((node1==nullptr) & (node2==nullptr))
                              return(new Digits(carry));

                              else
                              int tens=0;
                              int ones = valueOrZero(node1) + valueOrZero(node2) + carry;
                              if (ones >= 10)
                              tens = ones/10;
                              ones = ones % 10;

                              Digits* answer = new Digits(ones);
                              answer->next_node = addTwoNumbers(nextOrNull(node1), nextOrNull(node2), tens);
                              return answer;




                              void testAdd(int val1, int val2)
                              Digits *num1 = new Digits(val1);
                              Digits *num2 = new Digits(val2);
                              Digits *num3 = addTwoNumbers(num1, num2);
                              cout << digitsToString(num1) << "+" << digitsToString(num2) << " = " << digitsToString(num3) << endl;



                              int main(int argc, char* argv)
                              testAdd(1,1);
                              testAdd(8,9);
                              testAdd(23,8);
                              testAdd(999,25);






                              share|improve this answer























                              • The obvious flaws are error-handling and different length numbers.
                                – Deduplicator
                                Mar 30 at 13:24










                              • @Deduplicator, it's not obvious to me what you're referring to in your comment. Would you mind giving a little more detail?
                                – Alexander Hanysz
                                Mar 31 at 12:50










                              • 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?
                                – Deduplicator
                                Mar 31 at 13:07










                              • 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.
                                – Alexander Hanysz
                                Apr 1 at 0:41










                              • 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?
                                – Alexander Hanysz
                                Apr 2 at 5:15














                              up vote
                              1
                              down vote













                              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.)



                              Putting it all together, I get this version:



                              #include <iostream>
                              using namespace std;

                              struct Digits
                              Digits* next_node;
                              int value;

                              Digits(int arg)
                              if (arg < 10) value = arg; next_node = nullptr;
                              else value = arg % 10; next_node = new Digits(arg/10);

                              ;

                              int valueOrZero(Digits* node) return node==nullptr ? 0 : node->value;

                              Digits* nextOrNull(Digits* node) return node==nullptr ? nullptr : node->next_node;

                              string digitsToString(Digits* node)
                              return node==nullptr ? "" : digitsToString(node->next_node) + to_string(node->value);



                              Digits* addTwoNumbers(Digits* node1, Digits* node2, int carry=0)

                              if ((node1==nullptr) & (node2==nullptr))
                              return(new Digits(carry));

                              else
                              int tens=0;
                              int ones = valueOrZero(node1) + valueOrZero(node2) + carry;
                              if (ones >= 10)
                              tens = ones/10;
                              ones = ones % 10;

                              Digits* answer = new Digits(ones);
                              answer->next_node = addTwoNumbers(nextOrNull(node1), nextOrNull(node2), tens);
                              return answer;




                              void testAdd(int val1, int val2)
                              Digits *num1 = new Digits(val1);
                              Digits *num2 = new Digits(val2);
                              Digits *num3 = addTwoNumbers(num1, num2);
                              cout << digitsToString(num1) << "+" << digitsToString(num2) << " = " << digitsToString(num3) << endl;



                              int main(int argc, char* argv)
                              testAdd(1,1);
                              testAdd(8,9);
                              testAdd(23,8);
                              testAdd(999,25);






                              share|improve this answer























                              • The obvious flaws are error-handling and different length numbers.
                                – Deduplicator
                                Mar 30 at 13:24










                              • @Deduplicator, it's not obvious to me what you're referring to in your comment. Would you mind giving a little more detail?
                                – Alexander Hanysz
                                Mar 31 at 12:50










                              • 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?
                                – Deduplicator
                                Mar 31 at 13:07










                              • 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.
                                – Alexander Hanysz
                                Apr 1 at 0:41










                              • 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?
                                – Alexander Hanysz
                                Apr 2 at 5:15












                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              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.)



                              Putting it all together, I get this version:



                              #include <iostream>
                              using namespace std;

                              struct Digits
                              Digits* next_node;
                              int value;

                              Digits(int arg)
                              if (arg < 10) value = arg; next_node = nullptr;
                              else value = arg % 10; next_node = new Digits(arg/10);

                              ;

                              int valueOrZero(Digits* node) return node==nullptr ? 0 : node->value;

                              Digits* nextOrNull(Digits* node) return node==nullptr ? nullptr : node->next_node;

                              string digitsToString(Digits* node)
                              return node==nullptr ? "" : digitsToString(node->next_node) + to_string(node->value);



                              Digits* addTwoNumbers(Digits* node1, Digits* node2, int carry=0)

                              if ((node1==nullptr) & (node2==nullptr))
                              return(new Digits(carry));

                              else
                              int tens=0;
                              int ones = valueOrZero(node1) + valueOrZero(node2) + carry;
                              if (ones >= 10)
                              tens = ones/10;
                              ones = ones % 10;

                              Digits* answer = new Digits(ones);
                              answer->next_node = addTwoNumbers(nextOrNull(node1), nextOrNull(node2), tens);
                              return answer;




                              void testAdd(int val1, int val2)
                              Digits *num1 = new Digits(val1);
                              Digits *num2 = new Digits(val2);
                              Digits *num3 = addTwoNumbers(num1, num2);
                              cout << digitsToString(num1) << "+" << digitsToString(num2) << " = " << digitsToString(num3) << endl;



                              int main(int argc, char* argv)
                              testAdd(1,1);
                              testAdd(8,9);
                              testAdd(23,8);
                              testAdd(999,25);






                              share|improve this answer















                              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.)



                              Putting it all together, I get this version:



                              #include <iostream>
                              using namespace std;

                              struct Digits
                              Digits* next_node;
                              int value;

                              Digits(int arg)
                              if (arg < 10) value = arg; next_node = nullptr;
                              else value = arg % 10; next_node = new Digits(arg/10);

                              ;

                              int valueOrZero(Digits* node) return node==nullptr ? 0 : node->value;

                              Digits* nextOrNull(Digits* node) return node==nullptr ? nullptr : node->next_node;

                              string digitsToString(Digits* node)
                              return node==nullptr ? "" : digitsToString(node->next_node) + to_string(node->value);



                              Digits* addTwoNumbers(Digits* node1, Digits* node2, int carry=0)

                              if ((node1==nullptr) & (node2==nullptr))
                              return(new Digits(carry));

                              else
                              int tens=0;
                              int ones = valueOrZero(node1) + valueOrZero(node2) + carry;
                              if (ones >= 10)
                              tens = ones/10;
                              ones = ones % 10;

                              Digits* answer = new Digits(ones);
                              answer->next_node = addTwoNumbers(nextOrNull(node1), nextOrNull(node2), tens);
                              return answer;




                              void testAdd(int val1, int val2)
                              Digits *num1 = new Digits(val1);
                              Digits *num2 = new Digits(val2);
                              Digits *num3 = addTwoNumbers(num1, num2);
                              cout << digitsToString(num1) << "+" << digitsToString(num2) << " = " << digitsToString(num3) << endl;



                              int main(int argc, char* argv)
                              testAdd(1,1);
                              testAdd(8,9);
                              testAdd(23,8);
                              testAdd(999,25);







                              share|improve this answer















                              share|improve this answer



                              share|improve this answer








                              edited Apr 2 at 5:21


























                              answered Mar 30 at 12:46









                              Alexander Hanysz

                              1113




                              1113











                              • The obvious flaws are error-handling and different length numbers.
                                – Deduplicator
                                Mar 30 at 13:24










                              • @Deduplicator, it's not obvious to me what you're referring to in your comment. Would you mind giving a little more detail?
                                – Alexander Hanysz
                                Mar 31 at 12:50










                              • 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?
                                – Deduplicator
                                Mar 31 at 13:07










                              • 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.
                                – Alexander Hanysz
                                Apr 1 at 0:41










                              • 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?
                                – Alexander Hanysz
                                Apr 2 at 5:15
















                              • The obvious flaws are error-handling and different length numbers.
                                – Deduplicator
                                Mar 30 at 13:24










                              • @Deduplicator, it's not obvious to me what you're referring to in your comment. Would you mind giving a little more detail?
                                – Alexander Hanysz
                                Mar 31 at 12:50










                              • 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?
                                – Deduplicator
                                Mar 31 at 13:07










                              • 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.
                                – Alexander Hanysz
                                Apr 1 at 0:41










                              • 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?
                                – Alexander Hanysz
                                Apr 2 at 5:15















                              The obvious flaws are error-handling and different length numbers.
                              – Deduplicator
                              Mar 30 at 13:24




                              The obvious flaws are error-handling and different length numbers.
                              – Deduplicator
                              Mar 30 at 13:24












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




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












                              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?
                              – Deduplicator
                              Mar 31 at 13:07




                              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?
                              – Deduplicator
                              Mar 31 at 13:07












                              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.
                              – Alexander Hanysz
                              Apr 1 at 0:41




                              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.
                              – Alexander Hanysz
                              Apr 1 at 0:41












                              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?
                              – Alexander Hanysz
                              Apr 2 at 5:15




                              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?
                              – Alexander Hanysz
                              Apr 2 at 5:15












                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f190451%2fsum-numbers-represented-as-linked-lists%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Python Lists

                              Aion

                              JavaScript Array Iteration Methods