-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.AddTwoNumbers.js
More file actions
58 lines (57 loc) · 1.27 KB
/
2.AddTwoNumbers.js
File metadata and controls
58 lines (57 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
var temp = 0,v1=0;v2=0;
var ans = {val:-1,next:null};
var cur = ans;
while(l1||l2){
v1=l1?l1.val:0; v2=l2?l2.val:0;
temp += v1+v2;
cur.next = {val: temp%10,next:null};
temp = (temp-cur.next.val)/10;
if(l1) l1 = l1.next;
if(l2) l2 = l2.next;
if(cur.next) cur = cur.next;
}
if(temp) cur.next = {val:1,next:null};
return ans.next;
};
var addTwoNumbers = function(l1, l2) {
var temp = 0;
var ans = l1;
while(true){
temp = l1.val+l2.val+temp;
l1.val = temp%10;
temp = (temp - l1.val)/10;
if(l1.next!=null&&l2.next!=null){
l1 = l1.next;l2 = l2.next;
}else{
break;
}
}
if(l2.next){l1.next = l2.next;}
if(l1.next){
l1=l1.next;
while(true){
temp = l1.val+temp;
l1.val = temp%10;
temp = (temp - l1.val)/10;
if(l1.next)l1 = l1.next;
else break;
}
}
if(temp==1){
l1.next={val:1,next:null};
}
return ans;
};