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 Node { | |
constructor(value){ | |
this.value = value | |
this.left = null | |
this.right = null | |
} | |
} |
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 Node{ | |
constructor(val){ | |
this.val = val; | |
this.next = null; | |
} | |
} | |
class SinglyLinkedList{ | |
constructor(){ | |
this.head = null; |
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
// an undirected graph | |
class Graph{ | |
constructor(){ | |
// Graph has an adjacencyList attribute set as an object | |
this.adjacencyList = {} | |
} | |
addVertex(vertex){ | |
// to avoid overwriting the existing vertex, we need if statement |
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 ListNode(val, next) { | |
this.val = (val===undefined ? 0 : val) | |
this.next = (next===undefined ? null : 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
class Node { | |
constructor(value){ | |
this.value = value | |
this.left = null | |
this.right = null | |
} | |
} | |
class BinarySeachTree { | |
constructor(){ |
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 Node{ | |
constructor(val){ | |
this.val = val | |
this.next = null | |
} | |
} | |
class SinglyLinkedList{ | |
constructor(){ |
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 link: https://leetcode.com/problems/valid-parentheses/ | |
const isValid = (string) => { | |
// create a hashMap to store the valid match | |
const bracketsMap = { | |
'(': ')', | |
'{': '}', | |
'[': ']' | |
} | |
// create a stack data structure with array |
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
inOrderDFS(){ | |
// a variable to store the visited nodes | |
let result = [] | |
// helper function -- accepts a node | |
function traverse(node){ | |
// if node has left, recursion to find the leftest leaf node | |
if(node.left) traverse(node.left) | |
// push the node to the result | |
result.push(node) | |
// if node has right, recurstion to find the rightest leaf node |
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
const reverseList = (head, previous = null) => { | |
//base case | |
if(!head) return previous | |
//variable to store next node | |
let temp = head.next | |
//reverse the node | |
head.next = previous | |
//recursion starts | |
return reverseList(temp, head) |
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
const reverseList = (head) => { | |
//initiate previous as null | |
let previous = null | |
//initiate next without assigning | |
let next | |
//keep iteration while head is not null | |
while(head){ | |
//store the current node's next node | |
next = head.next | |
//reassign head's next with previous node |
NewerOlder