-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.html
More file actions
49 lines (49 loc) · 1.13 KB
/
2.html
File metadata and controls
49 lines (49 loc) · 1.13 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function ListNode(element) {
this.element = element;
this.next = null;
}
var addTwoNumbers = function(l1, l2) {
// 和的链表
let l3;
// 进位
let carry = 0;
while (l1 || l2 || carry) {
let element1; // 链表1的值
let element2; // 链表2的值
let sum = 0; // 和
if(l1) {
// 遍历l1
element1 = l1.element;
l1 = l1.next;
}
if(l2) {
// 遍历l2
element2 = l2.element;
l2 = l2.next;
}
// 每一位的和
sum = element1 + element2 + carry;
// 进位(返回小于等于x的最大整数)
carry = Math.floor(sum / 10);
// 和的链表
if(!l3) {
l3 = new Node(sum % 10);
} else {
l3.next = new Node(sum % 10);
l3 = l3.next
}
}
return l3
}
</script>
</body>
</html>