Skip to content

Commit 8240d37

Browse files
committed
update solution 002 Solution.js [JavaScript]
1 parent b793c1d commit 8240d37

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
var addTwoNumbers = function(l1, l2) {
14+
var c1 = l1,c2 = l2,c3,l3,carry = 0;
15+
while(c1||c2||carry){
16+
var v1 = 0,v2 = 0;
17+
if(c1){
18+
v1 = c1.val;
19+
c1 = c1.next;
20+
}
21+
if(c2){
22+
v2 = c2.val;
23+
c2 = c2.next;
24+
}
25+
var sum = v1 + v2 + carry;
26+
carry = (sum - sum%10)/10;
27+
if(!c3){
28+
l3 = new ListNode(sum%10);
29+
c3 = l3;
30+
}else{
31+
c3.next = new ListNode(sum%10);
32+
c3 = c3.next;
33+
}
34+
}
35+
return l3;
36+
}

0 commit comments

Comments
 (0)