Skip to content

Commit 38e498e

Browse files
authored
Refactor: JavaScript/lib (#68)
Creates a new directory `lib` within the `JavaScript` solutions directory to store re-usable functions and classes. The general pattern to follow would be: `JavaScript/lib/{{ github_handle }}/{{ category }}` So for the case of "Chapter 2: Linked Lists", I would add a new file `JavaScript/lib/avc278/linkedlist.js`.
1 parent 41686da commit 38e498e

File tree

4 files changed

+59
-78
lines changed

4 files changed

+59
-78
lines changed

JavaScript/chapter02/p01_remove_dups/avc278.js

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,10 @@
22
// Follow Up: How would you solve this problem if a temporary buffer is not allowed?
33

44
const assert = require("assert");
5-
6-
class LinkedListNode {
7-
constructor(val, next) {
8-
this.val = val === undefined ? null : val;
9-
this.next = next === undefined ? null : next;
10-
}
11-
}
12-
13-
const arrayToLinkedList = (arr) => {
14-
let tail = null;
15-
for (let i = arr.length - 1; i >= 0; i--) {
16-
tail = new LinkedListNode(arr[i], tail);
17-
}
18-
return tail;
19-
};
20-
21-
const compareLinkedLists = (A, B) => {
22-
if (!A && !B) return true;
23-
24-
while (A !== null && B !== null) {
25-
if (A.val !== B.val) return false;
26-
A = A.next;
27-
B = B.next;
28-
}
29-
30-
return !A && !B;
31-
};
5+
const {
6+
arrayToLinkedList,
7+
compareLinkedLists,
8+
} = require("../../lib/avc278/linkedlist");
329

3310
/**
3411
* Removes duplicates from a linked list
@@ -47,7 +24,11 @@ const removeDups1 = (list) => {
4724
const seen = new Set();
4825
let currNode = list;
4926
seen.add(currNode.val);
50-
for (let nextNode = currNode.next; nextNode.next !== null; nextNode = nextNode.next) {
27+
for (
28+
let nextNode = currNode.next;
29+
nextNode.next !== null;
30+
nextNode = nextNode.next
31+
) {
5132
if (seen.has(nextNode.val)) continue;
5233

5334
seen.add(nextNode.val);
@@ -109,15 +90,5 @@ removeDups.forEach((removeDup) => {
10990
removeDup(ll1);
11091
assert.ok(compareLinkedLists(ll1, expectedLL1));
11192
});
112-
it("should return false when comparing unequal linked lists", () => {
113-
assert.ok(
114-
!compareLinkedLists(arrayToLinkedList([]), arrayToLinkedList(["hi!"]))
115-
);
116-
});
117-
it("should return false when comparing another pair of unequal linked lists", () => {
118-
assert.ok(
119-
!compareLinkedLists(arrayToLinkedList([1, 2]), arrayToLinkedList([1]))
120-
);
121-
});
12293
});
12394
});

JavaScript/chapter02/p02_return_kth_to_last/avc278.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
11
// Return Kth to Last: Implement an algorithm to find the kth to last element of a singly linked list.
22

33
const assert = require("assert");
4-
5-
class LinkedListNode {
6-
constructor(val, next) {
7-
this.val = val === undefined ? null : val;
8-
this.next = next === undefined ? null : next;
9-
}
10-
}
11-
12-
const arrayToLinkedList = (arr) => {
13-
let tail = null;
14-
for (let i = arr.length - 1; i >= 0; i--) {
15-
tail = new LinkedListNode(arr[i], tail);
16-
}
17-
return tail;
18-
};
4+
const {
5+
arrayToLinkedList,
6+
} = require("../../lib/avc278/linkedlist");
197

208
/**
219
* Returns the kth to last node in a linked list

JavaScript/chapter02/p03_delete_middle_node/avc278.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,11 @@
55
// Result: nothing is returned, but the new linked list looks like a => b => d => e => f
66

77
const assert = require("assert");
8+
const {
9+
arrayToLinkedList,
10+
compareLinkedLists,
11+
} = require("../../lib/avc278/linkedlist");
812

9-
class LinkedListNode {
10-
constructor(val, next) {
11-
this.val = val === undefined ? null : val;
12-
this.next = next === undefined ? null : next;
13-
}
14-
}
15-
16-
const arrayToLinkedList = (arr) => {
17-
let tail = null;
18-
for (let i = arr.length - 1; i >= 0; i--) {
19-
tail = new LinkedListNode(arr[i], tail);
20-
}
21-
return tail;
22-
};
23-
24-
const compareLinkedLists = (A, B) => {
25-
if (!A && !B) return true;
26-
27-
while (A !== null && B !== null) {
28-
if (A.val !== B.val) return false;
29-
A = A.next;
30-
B = B.next;
31-
}
32-
return !A && !B;
33-
};
3413
/**
3514
* Deletes an inputted node somewhere in the middle of the linked list
3615
* @param {LinkedListNode} node input node to be removed

JavaScript/lib/avc278/linkedlist.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const assert = require("assert");
2+
3+
class LinkedListNode {
4+
constructor(val, next) {
5+
this.val = val === undefined ? null : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
const arrayToLinkedList = (arr) => {
11+
let tail = null;
12+
for (let i = arr.length - 1; i >= 0; i--) {
13+
tail = new LinkedListNode(arr[i], tail);
14+
}
15+
return tail;
16+
};
17+
18+
const compareLinkedLists = (A, B) => {
19+
if (!A && !B) return true;
20+
21+
while (A !== null && B !== null) {
22+
if (A.val !== B.val) return false;
23+
A = A.next;
24+
B = B.next;
25+
}
26+
27+
return !A && !B;
28+
};
29+
30+
describe(module.filename, () => {
31+
it("should return false when comparing unequal linked lists", () => {
32+
assert.ok(
33+
!compareLinkedLists(arrayToLinkedList([]), arrayToLinkedList(["hi!"]))
34+
);
35+
});
36+
it("should return false when comparing another pair of unequal linked lists", () => {
37+
assert.ok(
38+
!compareLinkedLists(arrayToLinkedList([1, 2]), arrayToLinkedList([1]))
39+
);
40+
});
41+
});
42+
43+
module.exports = { arrayToLinkedList, compareLinkedLists };

0 commit comments

Comments
 (0)