Skip to content

Instantly share code, notes, and snippets.

View Smakar20's full-sized avatar

Sohini Makar Smakar20

  • Rally Health Inc
  • San Francisco
View GitHub Profile
//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])
@Smakar20
Smakar20 / flattenBfs.js
Created December 27, 2017 18:09
flattenBfs created by smakar20 - https://repl.it/@smakar20/flattenBfs
//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){
//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){
@Smakar20
Smakar20 / removeAllDuplicates.js
Created December 27, 2017 00:23
removeAllDuplicates created by smakar20 - https://repl.it/@smakar20/removeAllDuplicates
/*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{
/*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){
/*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
@Smakar20
Smakar20 / reverseBetween.js
Created December 24, 2017 04:35
reverseBetween created by smakar20 - https://repl.it/@smakar20/reverseBetween
/*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){
@Smakar20
Smakar20 / reverseBetween.js
Created December 24, 2017 03:47
reverseBetween created by smakar20 - https://repl.it/@smakar20/reverseBetween
/*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){
@Smakar20
Smakar20 / removeNthFromEnd.js
Created December 23, 2017 20:17
removeNthFromEnd created by smakar20 - https://repl.it/@smakar20/removeNthFromEnd
//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)
//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)