-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.两数相加.js
44 lines (43 loc) · 891 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
42
43
44
/*
* @Author: qtx
* @Date: 2020-06-12 18:30:51
* @LastEditors: qtx
* @LastEditTime: 2020-06-12 18:39:11
* @Description:
*/
/*
* @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, curr = 0) {
if (l1 === null && l2 === null) {
if (curr) return new ListNode(curr)
} else {
if (l1 === null) l1 = new ListNode(0)
if (l2 === null) l2 = new ListNode(0)
let nextVal = (l2.val || 0) + (l1.val || 0) + curr
curr = 0
if (nextVal > 9) {
curr = 1
nextVal -= 10
}
l1.val = nextVal
l1.next = addTwoNumbers(l1.next, l2.next, curr)
return l1
}
}
// @lc code=end