Skip to content

Instantly share code, notes, and snippets.

View vadimkorr's full-sized avatar
🦉

Vadim vadimkorr

🦉
View GitHub Profile
// Reverse linked list
function reverse(dll) {
let curr = dll.head, prev = null
while(curr) {
let next = curr.next
curr.next = prev
prev = curr
curr = next
// Implement counting sort algorithm
// Complexity O(n+k)
// let toNum = (ch) => ch.charCodeAt(0);
// let toChar = (num) => String.fromCharCode(num);
// const RANGE = 255; // numbers, letters
let countingSort = function(input)
{
let inter = [];
@vadimkorr
vadimkorr / linked-list-as-stack.js
Created March 10, 2018 19:32
Implement Stack interface using linked list
// linked-list-as-stack
function stack() {
this.head = null;
}
stack.prototype.print = function() {
let curr = this.head
while(curr) {
console.log(curr.val)
@vadimkorr
vadimkorr / linked-list-as-queue.js
Last active March 10, 2018 19:40
Implement Queue interface using linked list
// linked-list-as-queue
function queue() {
this.head = null;
}
queue.prototype.print = function() {
console.log('print');
let curr = this.head
while(curr) {
@vadimkorr
vadimkorr / delete-from-linked-list.js
Last active March 10, 2018 19:58
Delete cell from linked list with respect given position
// Delete cell from linked list with respect given position
function lst() {
this.head = null;
}
lst.prototype.print = function() {
console.log('print');
let curr = this.head
while(curr) {
@vadimkorr
vadimkorr / bst-from-array.js
Created March 11, 2018 19:11
Construct binary search tree from sorted array
let node = function(data) {
this.data = data;
this.left = null;
this.right = null;
}
let bst = function() {
this.root = null;
}
@vadimkorr
vadimkorr / bst-to-dll.js
Created March 11, 2018 21:06
Construct sorted linked list from binary search tree
let node = function(data) {
this.data = data;
this.left = null;
this.right = null;
}
let head;
let BT2DLL = function(root)
{
@vadimkorr
vadimkorr / min-value-in-bst.js
Last active March 12, 2018 08:23
Find a minimal value in binary tree
// Find a minimal value in binary tree
let node = function(data) {
this.data = data;
this.left = null;
this.right = null;
}
let findMin = function(root) {
let curr = root;
@vadimkorr
vadimkorr / find-less-than-items.js
Last active March 13, 2018 09:01
Given two arrays A and B for each item in A find how many items in B are less than this item
let a = [5, 7, 8, 10]
let b = [1, 2, 3, 4, 5, 6, 7]
for (let i=0; i<a.length; i++) {
let k=0;
for (let j=0; j<b.length; j++) {
if (b[j] < a[i]) k++;
}
console.log(a[i] + ': ' + k + ' items');
k = 0;
@vadimkorr
vadimkorr / unique-char.js
Last active March 17, 2018 17:39
Find first non repeating char in a string
// Find first non repeating char in a string
function firstUnique(str) {
let dict = {};
for (let i=0; i<str.length; i++) {
if (dict[str[i]]) {
dict[str[i]] += 1;
} else {
dict[str[i]] = 1;