Skip to content

Commit c2ffd38

Browse files
author
Tushar Borole
committed
2. Add Two Numbers
1 parent a8b2eb0 commit c2ffd38

File tree

3 files changed

+102
-13
lines changed

3 files changed

+102
-13
lines changed

.idea/workspace.xml

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
| 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Solution](largest_element%20_in_an_array.js) | 68 ms | 35.9 MB | Medium | | | |
8080
| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Solution](search_a_2d_matrix.js) | 48 ms | 34.5 MB | Medium | | | |
8181
| 540 | [Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/) | [single_element_in_a_sorted_array.js](single_element_in_a_sorted_array.js) | 60 ms | 35.2 MB | Medium | | | |
82+
| 2 | [ Add Two Numbers](https://leetcode.com/problems/add-two-numbers) | [add_two_numbers.js](add_two_numbers.js) | | | Medium | | | |
8283
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [top_k_frequent_elements.js](top_k_frequent_elements.js) | 80 ms | 39.1 MB | Medium | | | |
8384
| 39 | [Combination Sum](https://leetcode.com/problems/combination-sum/) | [combination_sum.js](combination_sum.js) | 72 ms | 35.8 MB | Medium | | | |
8485
| 40 | [Combination Sum II](https://leetcode.com/problems/combination-sum-ii/) | [combination_sum_II.js](combination_sum_II.js) | 84 ms | 37.6 MB | Medium | | | |

add_two_numbers.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// https://leetcode.com/problems/add-two-numbers/
2+
function ListNode(val) {
3+
this.val = val;
4+
this.next = null;
5+
}
6+
7+
let arrayToList = function(totalArray) {
8+
let outNode = new ListNode(totalArray[0]);
9+
let current = outNode;
10+
11+
for (let i = 1; i < totalArray.length; i++) {
12+
current.next = new ListNode(totalArray[i]);
13+
current = current.next;
14+
}
15+
return outNode;
16+
};
17+
18+
/**
19+
* Definition for singly-linked list.
20+
* function ListNode(val) {
21+
* this.val = val;
22+
* this.next = null;
23+
* }
24+
*/
25+
/**
26+
* @param {ListNode} l1
27+
* @param {ListNode} l2
28+
* @return {ListNode}
29+
*/
30+
var addTwoNumbers = function(l1, l2) {
31+
let l1Array = [];
32+
const before = new ListNode();
33+
let tail = before;
34+
let c = 0;
35+
36+
while (l1 || l2 || c) {
37+
const v1 = l1 ? l1.val : 0;
38+
const v2 = l2 ? l2.val : 0;
39+
const v = v1 + v2 + c;
40+
41+
tail.next = new ListNode(v % 10);
42+
tail = tail.next;
43+
c = v >= 10 ? 1 : 0;
44+
l1 = l1 && l1.next;
45+
l2 = l2 && l2.next;
46+
}
47+
48+
return before.next;
49+
};
50+
51+
addTwoNumbers(
52+
arrayToList([
53+
1,
54+
0,
55+
0,
56+
0,
57+
0,
58+
0,
59+
0,
60+
0,
61+
0,
62+
0,
63+
0,
64+
0,
65+
0,
66+
0,
67+
0,
68+
0,
69+
0,
70+
0,
71+
0,
72+
0,
73+
0,
74+
0,
75+
0,
76+
0,
77+
0,
78+
0,
79+
0,
80+
0,
81+
0,
82+
0,
83+
1
84+
]),
85+
arrayToList([5, 6, 4])
86+
); //?

0 commit comments

Comments
 (0)