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

• ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
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;
}
}

• 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~~~

• 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 :)

ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
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;
}
}

• 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?

• 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);

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

• can you solve it using constant space?

• This post is deleted!

• @relive yeah

• 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

• 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;
}

• Due to the statement:

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?

• @ce2 very nice, thanks!

• @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) {}

• This post is deleted!

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