@luckyever619 consider this example number 235, this number is stored in linked list nodes : 5>3>2>Null
Add Two Numbers



Below is another solution with recursive call:
public static ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
if(l1==null)return l2;
else if(l2==null) return l1;
else
{
int val = l1.val+l2.val>=10?l1.val+l2.val10:l1.val+l2.val;
ListNode result = new ListNode(val);
result.next = addTwoNumbers(l1.next,l2.next);
if(l1.val+l2.val>=10)
result.next = addTwoNumbers(result.next,new ListNode(1));
return result;
}
}

@isaacjumac . So, result contains the head of the returning linkedlist right? What I don't understand is How can result point to the very first element of the linked list? result = new ListNode(0). so, result.val = 0 and result.next = null. Would you please help me to clarify my mistake. thanks

/**
 Definition for singlylinked list.
 function ListNode(val) {

this.val = val;

this.next = null;
 }
/
/*  @param {ListNode} l1
 @param {ListNode} l2
 @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
var p = l1;
var q = l2;
var carry = 0;
var head = new ListNode(0);
var result = head;
while(p!==null  q!== null){
var sum = 0;
var x = 0,y = 0;
if(p !== null) x = p.val;
if(q!==null) y = q.val;
sum = x + y + carry;
carry = Math.floor(sum/10);
result.next = new ListNode(sum%10);
result = result.next;
if(p!==null) p = p.next;
if(q!==null) q = q.next;
}
if(carry > 0){
result.next = new ListNode(carry);
}
return head.next;
};
//I can implementation in javascript.,and the Math.floor() is required.

@wangyiman said in Add Two Numbers:
Math.floor
you need round number , not Math.floor function.
function roundNumber(number){ return number(number%1); }

@liangminhua said in Add Two Numbers:
@wangyiman said in Add Two Numbers:
Math.floor
you need round number , not Math.floor function.
function roundNumber(number){ return number(number%1); }
How do you think that's different from
Math.floor
?

c++, rather than sum one digit by one, sum up and then generate the linked list.
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int n1 = 0, n2 = 0, i = 0, j = 0; while (l1){ n1 += l1>val*pow(10, i++); l1 = l1>next; } while (l2){ n2 += l2>val*pow(10, j++); l2 = l2>next; } int sum = n1 + n2; ListNode* res = new ListNode(1); ListNode* cur = res; while (sum){ cur>next = new ListNode(sum%10); sum /= 10; cur = cur>next; } return res>next; }

@jzhanggg you'll run into issues if the value of input or combined result is greater than max int which on a 64 bit machine is 9,223,372,036,854,775,807. (from google)

@nujohn, thanks a lot for pointing out that. This might be the reason they came up the problem.


javascript solution
var addTwoNumbers = function (l1, l2) { let newLinked = new ListNode(0), p = l1, q = l2, curr = newLinked, carry = 0; while(p!==nullq!==null){ let x = p!==null?p.val:0, y = q!==null?q.val:0, sum = carry + x + y; carry = Math.floor(sum / 10); curr.next = new ListNode(sum % 10); curr = curr.next; p!==null?p=p.next:null; q!==null?q=q.next:null; } if(carry>0){ curr.next = new ListNode(carry); } return newLinked.next; };

Has anyone been able to successfully submit a solution in Go? Can you please share your solution? My solution works for me locally, but the same testcase throws a runtime error on leetcode.
This is my solution:
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
return addTwoNumebersUtil(l1, l2, 0)
}func addTwoNumebersUtil(l1 *ListNode, l2 *ListNode, carry int) *ListNode {
if l1 == nil && l2 == nil {
if carry == 0 {
return nil
} else {
return &ListNode{Val : carry, Next : nil}
}
}var v1, v2 int if l1 == nil { l1 = &ListNode{Val : 0, Next : nil} } v1 = l1.Val if l2 == nil { l1 = &ListNode{Val : 0, Next : nil} } v2 = l2.Val s := v1 + v2 + carry n := addTwoNumebersUtil(l1.Next, l2.Next, s / 10) return &ListNode {Val : s % 10, Next : n}
}