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
//Sorting an array. | |
(function sort(inp){ | |
if(inp.length == 0 || inp.length == 1) return arr | |
var opArr = [], | |
arr = inp.slice() | |
while(inp.length != opArr.length){ | |
opArr.push(findMax(arr)) | |
var idx = arr.indexOf(opArr[opArr.length - 1]) |
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
//Given a binary tree, flatten it to a linked list levelwise. | |
class Tree{ | |
constructor(val){ | |
this.val = val | |
this.left = null | |
this.right = null | |
} | |
insertLeft(val){ |
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
//Given a binary tree, flatten it to a linked list. | |
class Tree{ | |
constructor(val){ | |
this.val = val | |
this.left = null | |
this.right = null | |
} | |
insertLeft(val){ |
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
/*Remove duplicates and return only distinct numbers from the original array.*/ | |
(function removeAllDuplicates(arr){ | |
var count = 1 | |
var opArr = [arr[0]] | |
for(var i = 1; i < arr.length; i++){ | |
if(arr[i] == arr[i-1]){ | |
count += 1 | |
opArr.push(arr[i]) | |
}else{ |
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
/*Print all the node values in different levels (left to right order) of a binary tree. Each level should be printed in new line.*/ | |
class Tree{ | |
constructor(val){ | |
this.val = val | |
this.left = null | |
this.right = null | |
} | |
insertLeft(val){ |
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
/*reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… | |
You must do this in-place without altering the nodes' values. | |
For example, | |
Given {1,2,3,4}, reorder it to {1,4,2,3}*/ | |
class Node{ | |
constructor(val, next){ | |
this.val = val |
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
/*Reverse a linked list from position m to n. Do it in-place and in | |
one-pass. | |
For example: | |
Given 1->2->3->4->5->NULL, m = 2 and n = 4, | |
return 1->4->3->2->5->NULL.*/ | |
class Node{ | |
constructor(val, 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
/*Reverse a linked list from position m to n. Do it in-place and in | |
one-pass. | |
For example: | |
Given 1->2->3->4->5->NULL, m = 2 and n = 4, | |
return 1->4->3->2->5->NULL.*/ | |
class Node{ | |
constructor(val, 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
//remove nth node from the last. | |
function Node(val, next){ | |
this.val = val | |
this.next = next | |
} | |
//1->2->3->4->5 | |
var n1 = new Node(5,null) | |
var n2 = new Node(4,n1) | |
var n3 = new Node(3,n2) | |
var n4 = new Node(2,n3) |
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
//check of the given linked list is a palindrome. | |
function Node(val, next){ | |
this.val = val | |
this.next = next | |
} | |
//1->2->3->4->5 | |
var n1 = new Node(5,null) | |
var n2 = new Node(4,n1) | |
var n3 = new Node(3,n2) | |
var n4 = new Node(4,n3) |