This file contains hidden or 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 linked list | |
| function reverse(dll) { | |
| let curr = dll.head, prev = null | |
| while(curr) { | |
| let next = curr.next | |
| curr.next = prev | |
| prev = curr | |
| curr = next |
This file contains hidden or 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
| // 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 = []; |
This file contains hidden or 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
| // linked-list-as-stack | |
| function stack() { | |
| this.head = null; | |
| } | |
| stack.prototype.print = function() { | |
| let curr = this.head | |
| while(curr) { | |
| console.log(curr.val) |
This file contains hidden or 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
| // linked-list-as-queue | |
| function queue() { | |
| this.head = null; | |
| } | |
| queue.prototype.print = function() { | |
| console.log('print'); | |
| let curr = this.head | |
| while(curr) { |
This file contains hidden or 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
| // 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) { |
This file contains hidden or 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
| let node = function(data) { | |
| this.data = data; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| let bst = function() { | |
| this.root = null; | |
| } |
This file contains hidden or 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
| let node = function(data) { | |
| this.data = data; | |
| this.left = null; | |
| this.right = null; | |
| } | |
| let head; | |
| let BT2DLL = function(root) | |
| { |
This file contains hidden or 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
| // 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; |
This file contains hidden or 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
| 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; |
This file contains hidden or 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
| // 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; |