[c++] Sharing my 11-line c++ solution, can someone make it even more concise?


  • 125
    C
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode preHead(0), *p = &preHead;
        int extra = 0;
        while (l1 || l2 || extra) {
            int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + extra;
            extra = sum / 10;
            p->next = new ListNode(sum % 10);
            p = p->next;
            l1 = l1 ? l1->next : l1;
            l2 = l2 ? l2->next : l2;
        }
        return preHead.next;
    }

  • 0
    R

    like this:
    declare one more ListNode* variable (e.g. temp) and~~

    int sum = ((l1)? (temp=l1, l1=l1->next, temp->val):0) + ...(same for l2)
    Hahahaha~~~


  • 7
    W

    Is it important to make the code more concise? I think its enough, making the code more readable, clear is the most important. Your code is concise and clear enough :)


  • 34

    How about this?

    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode preHead(0), *p = &preHead;
        int extra = 0;
        while (l1 || l2 || extra) {
            if (l1) extra += l1->val, l1 = l1->next;
            if (l2) extra += l2->val, l2 = l2->next;
            p->next = new ListNode(extra % 10);
            extra /= 10;
            p = p->next;
        }
        return preHead.next;
    }
    

  • 0
    T

    Hi, I'm a beginner in C. I wanted to know how do we write this statement in C.

    p->next = new ListNode(sum % 10);

    We need to make use of malloc right but I'm not understanding how do we put sum%10?


  • 4
    C

    C does not have class or C++ struct, so you can only use C struct, and it has no constructor nor methods. So ListNode will look like

    struct ListNode {
        int val;
        struct ListNode *next;
    };
    

    and we would create a function to create and initialize a ListNode

    struct ListNode *list_node_new(int x) {
        struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
        node->next = NULL;
        node->val = x;
    }
    

    then we can

    p->next = list_node_new(sum % 10);
    

  • 0
    S

    Could you please elaborately explain your program? It seems like an excellent solution but I need documentation for this answer


  • 0
    L

    can you solve it using constant space?


  • 0
    B

    @ce2 nice answer!!


  • 0

    @StefanPochmann j'adore


  • 0
    D
    This post is deleted!

  • 0
    D

    @relive yeah


  • 0
    J

    I'm new to C++ I'm wondering that this line

    p->next = new ListNode(sum % 10);
    

    allocat memory in heap, do we need to have some memory management to avoid memory leak


  • 4
    C

    I think this is about as concise as you can get. Use the sentinel val field for the sum and carry; condense an assignment into the while condition and another assignment into the current node assignment.

    /**
         * Concise solution.  Uses sentinel val field as sum and carry.
         * 
         * NOTE:  (sentinal /= 10) needs to be the first operand of while condition.
         *        Otherwise, boolean expression can short-circuit when l1 or l2
         *        are not null, causing sentinel.val to not be decreased for the
         *        next iteration.
         */
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
            ListNode sentinel(0), *c = &sentinel;
            while((sentinel.val /= 10) || l1 || l2) {
                if(l1) sentinel.val += l1->val, l1 = l1->next;
                if(l2) sentinel.val += l2->val, l2 = l2->next;
                c = (c->next = new ListNode(sentinel.val % 10));
            }
            return sentinel.next;
        }
    

  • 1

    Due to the statement:

    ListNode preHead(0), *p = &preHead;
    

    the address of preHead is stored in the pointer variable p. In other words, p points to preHead. What I don't understand is, how (and why) do we return preHead.next at the end?


  • 1
    C

    @BatCoder preHead is a dummy node with a next field that points to the head of the list. Once we are done constructing the linked list containing the desired sum, we wish to return the head of the linked list that we just constructed. We can't access the head of the linked list via the pointer p since we used p to construct the new linked list, hence p no longer points to the preHead node nor is it likely that p points to the head of the list. However, we still have the unaltered preHead node whose next field points to the head of the list! Therefore, we can simply return the head of the linked list via

    preHead.next
    

  • 0
    L

    @ce2 very nice, thanks!


  • 0
    P

    @tanmays ListNode has a constructor ListNode(int x) and in c++ it is new which dynamically allocates

    struct ListNode {

    • int val;
      
    • ListNode *next;
      
    • ListNode(int x) : val(x), next(NULL) {}

  • 0
    L

  • -1
    C
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.