-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.两数相加.js
63 lines (52 loc) · 1.26 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* @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) {
let left = l1
let right = l2
let ld = null
let next = null
while (left !== null || right !== null) {
const l1Value = left ? left.val : 0
const l2Value = right ? right.val : 0
const current = l1Value + l2Value
left = left ? left.next : null
right = right ? right.next : null
if (!ld) {
let tmpValue = current % 10
let nextValue = Math.floor(current / 10)
ld = new ListNode(tmpValue)
if (left || right || nextValue) {
next = new ListNode(nextValue)
ld.next = next
}
} else {
let tmpValue = next.val + current
next.val = tmpValue % 10
let nextValue = Math.floor(tmpValue / 10)
// 还需要下一个时
if (left || right || nextValue) {
const tmp = new ListNode(nextValue)
next.next = tmp
next = tmp
}
}
}
return ld
};
// @lc code=end