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
const reverseList = (head, previous = null) => { | |
//base case | |
if(!head) return previous | |
//variable to store next node | |
let temp = head.next | |
//reverse the node | |
head.next = previous | |
//recursion starts | |
return reverseList(temp, head) |
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
const reverseList = (head) => { | |
//initiate previous as null | |
let previous = null | |
//initiate next without assigning | |
let next | |
//keep iteration while head is not null | |
while(head){ | |
//store the current node's next node | |
next = head.next | |
//reassign head's next with previous node |
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
function ListNode(val, next) { | |
this.val = (val===undefined ? 0 : val) | |
this.next = (next===undefined ? null : 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
const sortedSquares = (A) => { | |
let result = [] //empty array to push new value | |
let start = 0 //beginning index of the array | |
let end = A.length - 1 // end index of the array | |
let position = end //the position in result array of new value | |
while (start <= end) { //start point can't be smaller than end point | |
if (A[start] ** 2 > A[end] ** 2) { //if squared start index value is | |
//greater than end index value | |
result[position--] = A[start++] ** 2 //put it from the end of the result array |
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
const sortedSquares = (A) => { | |
return A.map((ele) => { // transform the given array | |
return ele*ele //square the element | |
}).sort((a,b) => { | |
return a-b // sort by ascending order | |
}) | |
}; |
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
const findJudge = (N, trust) => { | |
// keep track of how many likes the element gives | |
let likesCountList = {} | |
//keep track of how many likes the element receives | |
let beingLikedCountList = {} | |
//hash the key from 1 to N | |
for(let i = 1; i <= N; i++){ | |
likesCountList[i] = 0 | |
beingLikedCountList[i] = 0 |
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
const mergeTrees = (t1, t2) => { | |
//when there is null given tree | |
if(!t1 || !t2) return t1 || t2 | |
//both of trees are not null, | |
//we sum the value, and transform t1, | |
//we can also choose t2, and return t2 at the end | |
t1.val += t2.val | |
//recursively traverse left path |
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
const hasCycle = (head) => { | |
// initial with fast and slow pointers with head | |
let slow = head | |
let fast = head | |
//traverse linked list | |
while(fast && fast.next){ | |
// fast moves by two | |
fast = fast.next.next | |
// slow moves by one | |
slow = slow.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
const hasCycle = (head) => { | |
// Set variable to store distinct nodes | |
let set = new Set() | |
// for traversing, initialize current with head node | |
let current = head | |
//traverse the linked list | |
while(current){ | |
if(set.has(current)){ | |
// if duplication occurs, return true |
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
let movie1 = function () {} | |
movie1.prototype.description = function() { | |
return "description of movie1" | |
} | |
let movie2 = function() {} | |
movie2.prototype = Object.create(movie1.prototype); | |
movie2.prototype.description = function() { | |
return "description of movie2" | |
} | |
let movie3 = function() {} |
NewerOlder