Leetcode题解之 —— 两数相加

思路


  • 多指针
  • 对应相加
  • 注意增量

题解


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
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
const result = new ListNode(0);
let [current, incremental] = [result, 0];

while (l1 || l2) {
const [v1, v2] = [
l1 ? l1.val : 0,
l2 ? l2.val : 0,
];

const sum = v1 + v2 + incremental;
current = current.next = new ListNode(sum % 10);
incremental = ~~(sum / 10);

[l1, l2] = [l1 ? l1.next: null, l2 ? l2.next : null];
}

if (incremental) {
current.next = new ListNode(incremental);
}

return result.next;
};