-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddTwoNumbers.js
108 lines (98 loc) · 1.86 KB
/
addTwoNumbers.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* @Author: Z1hgq
* @Date: 2019-09-16 14:13:15
* @LastEditTime: 2019-09-17 14:16:55
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
const addTwoNumbers = function (l1, l2) {
// let arrl1 = [];
// let arrl2 = [];
// while(l1){
// arrl1.push(l1.val);
// l1 = l1.next;
// }
// while(l2){
// arrl2.push(l2.val);
// l2 = l2.next;
// }
// l1 = null;
// l2 = null;
// arrl1 = arrl1.reverse();
// arrl2 = arrl2.reverse();
// let strl1 = '';
// let strl2 = '';
// for(let num of arrl1){
// strl1 += num;
// }
// for(let num of arrl2){
// strl2 += num;
// }
// arrl1 = null;
// arrl2 = null;
let strl1 = '';
let strl2 = '';
while (l1) {
strl1 += l1.val;
l1 = l1.next;
}
while (l2) {
strl2 += l2.val;
l2 = l2.next;
}
strl1 = strl1.split('').reverse().join('');
strl2 = strl2.split('').reverse().join('');
// 注意int有最大安全范围,超过这个范围的运算应使用BigInt,BigInt不受安全范围限制
const res = (BigInt(strl1) + BigInt(strl2)).toString();
strl1 = null;
strl2 = null;
const resNode = {
val: res[res.length - 1],
next: null,
};
// 构造链表
for (let i = res.length - 2; i >= 0; i--) {
let node = {
val: res[i],
next: null
};
let currentNode = resNode;
while (currentNode.next) {
currentNode = currentNode.next;
}
currentNode.next = node;
node = null;
}
return resNode;
};
const l1 = {
val: '1',
next: {
val: '2',
next: {
val: '3',
next: null
}
}
};
const l2 = {
val: '1',
next: {
val: '2',
next: {
val: '3',
next: null
}
}
};
console.log(addTwoNumbers(l1, l2));