Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9538bea

Browse files
Added utils class
1 parent b7d7a37 commit 9538bea

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

LeetcodeProblems/Add_Two_Numbers.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
Add Two Numbers
33
https://leetcode.com/problems/add-two-numbers/
44
5-
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
5+
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order
6+
and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
67
78
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
89
@@ -20,16 +21,19 @@ Explanation: 342 + 465 = 807.
2021
* this.next = null;
2122
* }
2223
*/
24+
25+
var ListNode = require('../utilsClasses/ListNode').ListNode;
26+
2327
/**
2428
* @param {ListNode} l1
2529
* @param {ListNode} l2
2630
* @return {ListNode}
2731
*/
28-
32+
2933
var addTwoNumbers = function(l1, l2) {
30-
if(l1 === null) {
34+
if(l1 === null)
3135
return (l2 === null) ? new ListNode(0) : l2;
32-
} else if(l2 === null)
36+
else if(l2 === null)
3337
return l1;
3438

3539
var elem = l1.val + l2.val;
@@ -62,3 +66,12 @@ var addTwoNumbers = function(l1, l2) {
6266
}
6367
return head;
6468
};
69+
70+
71+
var main = function() {
72+
const list1 = ListNode.linkenList([1,2,3,4]);
73+
const list2 = ListNode.linkenList([1,2,3,4]);
74+
console.log(addTwoNumbers(list1, list2));
75+
}
76+
77+
main();

LeetcodeProblems/BinaryGap.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
Binary Gap
23
https://leetcode.com/problems/binary-gap/description/
34
45
Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N.

Main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var main = async function() {
99
for(i in problems) {
1010
console.log("Solving: " + problems[i] + ":");
1111
const problem = require(PROBLEMS_FOLDER + problems[i]);
12+
1213
if (typeof(problem.main) !=='undefined') {
1314
problem.main();
1415
console.log("End of the solution for : " + problems[i] + ",\n\n");

utilsClasses/ListNode.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class ListNode {
2+
constructor(val) {
3+
this.val = val;
4+
this.next = null;
5+
}
6+
7+
static linkenList(arr) {
8+
if(arr.length === 0)
9+
return null;
10+
11+
var first = new ListNode(arr[0]);
12+
var head = first;
13+
for(var i = 1; i <= arr.length; i++) {
14+
head.next = new ListNode(arr[i]);
15+
head = head.next;
16+
}
17+
18+
return first;
19+
}
20+
}
21+
22+
module.exports.ListNode = ListNode;

0 commit comments

Comments
 (0)