This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function reverseLinkedList(head) { | |
let prev = null; | |
let cur = head; | |
let next = null; | |
while(cur) { | |
next = cur.next; | |
cur.next = prev; | |
prev = cur; | |
cur = next; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = []; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using strategy of slower and faster pointers | |
class Node { | |
constructor(data) { | |
this.data = data; | |
this.next = null; | |
} | |
} | |
function hasCycle(head) { |