Last active
November 26, 2021 20:11
-
-
Save samsonajulor/b8228e99740205be6b03ee506586ec5a to your computer and use it in GitHub Desktop.
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
//flat an array | |
function flat(arr, depth) { | |
if (depth > 0) { | |
return arr.reduce( | |
(acc, val) => acc.concat(Array.isArray(val) ? flat(val, depth - 1) : val), | |
[] | |
) | |
} else { | |
return arr.slice() | |
} | |
} | |
let arr1 = [1, [2, 3], [3, [4, 5]], 4, 5] | |
// console.log(flat(arr1, 'infinity')) | |
console.log(flat(arr1, 2)) | |
//find the sise of a linked list | |
sise() { | |
this.count = 0 | |
let current = this.head | |
while(current) { | |
this.count++ | |
current = current.next | |
} | |
return this.count | |
} | |
deleteTail() { | |
if (this.head === this.tail) { | |
this.head = this.tail = null | |
} else { | |
this.tail = this.tail.prev | |
this.tail.next = null | |
} | |
insertBefore(val, existing) { | |
let current = this.head | |
while (current){ | |
if (current.value === existing) { | |
break; | |
} | |
current = current.next | |
} | |
let newNode = new Node(val) | |
let prevNode = current.prev | |
prevNode.next = newNode | |
newNode.next = current | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment