-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2. Add Two Numbers.ts
75 lines (66 loc) · 1.79 KB
/
2. Add Two Numbers.ts
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
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
{
class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
}
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
const list1: number[] = [];
const list2: number[] = [];
while ((l1 && (l1.val || l1.val === 0)) || (l2 && (l2.val || l2.val === 0))) {
if (l1) {
list1.push(l1.val);
}
if (l2) {
list2.push(l2.val);
}
if (l1 && !l1.next && l2 && !l2.next) {
break;
}
if (l1 && l1.next) {
l1 = l1.next;
} else {
l1 = new ListNode();
}
if (l2 && l2.next) {
l2 = l2.next;
} else {
l2 = new ListNode();
}
}
const answerNumList = Array.from({ length: list1.length }, () => 0);
for (let i = 0; i < list1.length; i++) {
const sum = +list1[i] + +list2[i];
answerNumList[i] += sum;
if (answerNumList[i] > 9) {
answerNumList[i] = answerNumList[i] % 10;
if (i === list1.length - 1) {
answerNumList.push(1);
} else {
answerNumList[i + 1] += 1;
}
}
}
let node = new ListNode(answerNumList[answerNumList.length - 1]);
for (let i = answerNumList.length - 2; i >= 0; i--) {
const newNode = new ListNode(answerNumList[i]);
newNode.next = node;
node = newNode;
}
return node;
};
}