Skip to content

Instantly share code, notes, and snippets.

@CarlaTeo
CarlaTeo / reverseLinkedList.js
Created May 27, 2021 03:33
[JS] Reverse linked list
function reverseLinkedList(head) {
let prev = null;
let cur = head;
let next = null;
while(cur) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
@CarlaTeo
CarlaTeo / buildBST.js
Created May 24, 2021 02:12
[JS] Build BST from sorted array
function buildBST(sortedArray) {
if(!sortedArray.length) return null;
if(sortedArray.length === 1) return new Node(sortedArray[0]);
const rootIndex = Math.floor(sortedArray.length / 2);
const root = new Node(sortedArray[rootIndex]);
root.left = buildBST(sortedArray.slice(0, rootIndex));
root.right = buildBST(sortedArray.slice(rootIndex + 1));
return root;
@CarlaTeo
CarlaTeo / BSTSmallestGenerator.js
Created May 18, 2021 01:55
[JS ] Binary Search Tree iterator to find the next smallest number
class BSTIterator {
constructor(root) {
this.nextValue = null;
this.sortedValues = getInOrderValues(root);
}
hasNext() {
if(!this.nextValue) {
const next = this.sortedValues.next();
this.nextValue = next.done ? null : next.value;
@CarlaTeo
CarlaTeo / binaryTreeHeight.js
Created May 17, 2021 03:08
[JS] Binary tree height
// Problem: https://www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem
function getHeight(head) {
if(!head) return -1;
const heights = new Set();
DFS(head, -1, heights);
return Math.max(...heights);
}
@CarlaTeo
CarlaTeo / colorfulNumbers.js
Created May 17, 2021 02:35
[JS] Colorful Numbers
// Source problem: https://algorithms.tutorialhorizon.com/colorful-numbers/
// Objective: Given a number, find out whether its colorful or not.
// Colorful Number: When in a given number, product of every digit of a sub-sequence are different. That number is called Colorful Number.
// Given Number : 3245
// Output : Colorful
// Number 3245 can be broken into parts like 3 2 4 5 32 24 45 324 245.
// this number is a colorful number, since product of every digit of a sub-sequence are different.
// That is, 3 2 4 5 (3*2)=6 (2*4)=8 (4*5)=20, (3*2*4)= 24 (2*4*5)= 40
// Given Number : 326
@CarlaTeo
CarlaTeo / heapSort.js
Last active June 11, 2021 17:17
[JS] Heap sort
function heapSort(array) {
const heap = heapify(array);
let lengthToConsider = array.length;
for(let i = array.length - 1; i > 0; i--) {
swap(array, i, 0);
lengthToConsider--;
descendHeap(array, lengthToConsider, 0);
}
}
@CarlaTeo
CarlaTeo / quickSort.js
Created May 16, 2021 21:03
[JS] Quick Sort
function quickSort(array) {
if(array.length <= 1) return array;
const pivot = array[0];
const [left, right] = pivotDivision(array.slice(1), pivot);
return [...quickSort(left), pivot, ...quickSort(right)];
}
function pivotDivision(array, pivot) {
const left = [];
@CarlaTeo
CarlaTeo / mergeSort.js
Created May 16, 2021 20:53
[JS] Merge Sort
function mergeSort(array) {
if(array.length <= 1) return array;
const half = Math.floor(array.length / 2);
const left = array.slice(0, half);
const right = array.slice(half);
const sortedLeft = mergeSort(left);
const sortedRight = mergeSort(right);
return merge(sortedLeft, sortedRight);
@CarlaTeo
CarlaTeo / bucketSort.js
Created May 16, 2021 20:29
[JS] Bucket sort
// Bucket Sort: O(N) method to sort natural numbers
function bucketSort(array) {
const buckets = [];
array.forEach(number => {
if(!buckets[number]) buckets[number] = 1;
else buckets[number] += 1;
})
const sortedArray = [];
buckets.forEach((counter, number) => {
@CarlaTeo
CarlaTeo / checkCycleInLikedList.js
Created May 16, 2021 16:18
[JS] Check if a linked list has a cycle
// Using strategy of slower and faster pointers
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function hasCycle(head) {