-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.js
37 lines (35 loc) · 788 Bytes
/
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
/**
* 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 head = tail = new ListNode((l1.val + l2.val) % 10)
let add = 0
while (l1 || l2) {
const n1 = l1 ? l1.val : 0
const n2 = l2 ? l2.val : 0
const sum = n1 + n2 + add
tail.next = new ListNode(sum % 10)
tail = tail.next
add = Math.floor(sum / 10)
l1 = l1?.next
l2 = l2?.next
}
if (add) {
tail.next = new ListNode(add)
}
return head.next
};
/*
2021/9/16
86 84
模拟 O(N)
*/