-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.两数相加.js
46 lines (41 loc) · 1.04 KB
/
2.两数相加.js
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
/*
* @lc app=leetcode.cn id=2 lang=javascript
*
* [2] 两数相加
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function (l1, l2) {
const newList = new ListNode();
let result = newList;
while ((l1 && !isNaN(l1.val)) || (l2 && !isNaN(l2.val))) {
console.log(!isNaN(l1.val));
console.log(!isNaN(l2.val));
const sum = (l1.val || 0) + (l2.val || 0) + (result.val || 0);
if (sum >= 10) {
result.val = sum % 10;
result.next = new ListNode(1);
} else {
result.val = sum;
}
if ((l1.next || l2.next) && !result.next) {
result.next = new ListNode();
}
l1 = l1.next || {};
l2 = l2.next || {};
result = result.next;
}
return newList;
};
// @lc code=end