-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-two-numbers_0518.html
45 lines (44 loc) · 1.07 KB
/
add-two-numbers_0518.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>add-two-numbers</title>
</head>
<body></body>
<script>
/**
* 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 dummy = new ListNode();
let temp = dummy;
let sum = 0;
while (l1 || l2 || sum) {
if (l1) {
sum += l1.val;
l1 = l1.next;
}
if (l2) {
sum += l2.val;
l2 = l2.next;
}
const remainder = sum % 10;
sum = Math.floor(sum / 10);
temp.next = new ListNode(remainder);
temp = temp.next;
}
return dummy.next;
};
</script>
</html>