Skip to content

Instantly share code, notes, and snippets.

View GAierken's full-sized avatar
🦄
Creating.

Gulgina Arkin GAierken

🦄
Creating.
View GitHub Profile
class Node {
constructor(value){
this.value = value
this.left = null
this.right = null
}
}
class Node{
constructor(val){
this.val = val;
this.next = null;
}
}
class SinglyLinkedList{
constructor(){
this.head = null;
@GAierken
GAierken / graph.js
Last active November 21, 2022 08:42
// 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
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
class Node {
constructor(value){
this.value = value
this.left = null
this.right = null
}
}
class BinarySeachTree {
constructor(){
class Node{
constructor(val){
this.val = val
this.next = null
}
}
class SinglyLinkedList{
constructor(){
// 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
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
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)
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