-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path445.两数相加-ii.js
68 lines (64 loc) · 1.4 KB
/
445.两数相加-ii.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
63
64
65
66
67
68
/*
* @lc app=leetcode.cn id=445 lang=javascript
*
* [445] 两数相加 II
*/
// @lc code=start
/**
* 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) {
let s1 = [], s2 = []
let p1 = l1, p2 = l2
while (p1) {
s1.push(p1.val)
p1 = p1.next
}
while (p2) {
s2.push(p2.val)
p2 = p2.next
}
let ans = new ListNode(null), carry = 0
while (s1.length > 0 || s2.length > 0) {
let n = 0
if (s1.length > 0) n += s1.pop()
if (s2.length > 0) n += s2.pop()
n += carry
if (n < 10) carry = 0
else {
carry = Math.floor(n / 10)
n = n % 10
}
let node = new ListNode(n)
node.next = ans.next
ans.next = node
}
if (carry > 0) {
let node = new ListNode(carry)
node.next = ans.next
ans.next = node
}
return ans.next
};
// @lc code=end
// function ListNode(val) {
// this.val = val;
// this.next = null;
// }
// let l1 = new ListNode(5)
// l1.next = new ListNode(2)
// l1.next.next = new ListNode(4)
// l1.next.next.next = new ListNode(3)
// let l2 = new ListNode(5)
// l2.next = new ListNode(6)
// l2.next.next = new ListNode(4)
// addTwoNumbers(l1, l2)