-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.两数相加.js
41 lines (37 loc) · 807 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
38
39
40
41
/*
* @lc app=leetcode.cn id=2 lang=javascript
*
* [2] 两数相加
*/
// @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 header = new ListNode(-1);
let h = header,
h1 = l1,
h2 = l2,
carry = 0;
while (h1 || h2 || carry) {
let num1 = h1 === null ? 0 : h1.val,
num2 = h2 === null ? 0 : h2.val;
let num = (num1 + num2 + carry) % 10;
carry = Math.floor((num1 + num2 + carry) / 10);
h.next = new ListNode(num);
h = h.next;
h1 = h1 === null ? null : h1.next;
h2 = h2 === null ? null : h2.next;
}
return header.next;
};
// @lc code=end