Im confused on how head.next returns the entire list instead of a next value such l1,l2,dummy .next does in the code below. particularly I’m wondering how head.next returns an entire sorted array and skips the -1 value that was input on the second line. Answer Maybe it helps when visualising how the list is built: Let the input be a
Tag: algorithm
Traversing recursively through an array and modifying values of object properties in JavaScript
The post may seem lengthy but it’s quite easy to follow, if not, I will add more details I have criteria array which that looks like : Characteristics of criteria: It could have nested arrays. First item array (or nested array) is always going to be either “and” or “or” Second item onwards in array, item could be either an
How can i get the number without repetition?
i want to get the number with only one repitition: i want to get [1,2,1], but right now i am getting only [2] Answer Just overwrite your original array using Array.prototype.filter:
Build tree from edge pairs and root
I’m trying to write a program that takes an array of edge pairs and turns it into a tree. I’m given a root. In this example, the root is 2. The only constraint is, each node can max have 2 children. Sample input: Expected output: Would look something like this: This is my attempt so far: Feel like this should
how to generate spiral matrix in javascript?
I am trying to generate sprial matrix in javascript. question Given an integer A, generate a square matrix filled with elements from 1 to A^2 in spiral order. input : 3 when input is 4 my approach is to create 2d array with 0 value and after that they will fill values. Answer You could take loops for each edges
Improve speed of my binary search algorithm
I have written a binary search algorithm in JavaScript: I wanted to ask if I can improve this algorithm to search faster or if some mistake is made here? EDIT: thank you guys for your help, this solution should work correctly now: Answer You are taking values as indices. If you take greater values than indices, you see your codes
How do i find character position (row, col) in multi-line string?
How i can find position of current character in multi-line string? If every line in a string was the same length it would be easy. E.g. But how can i do it, if every row is different length? E.g. Answer Use a while loop to iterate over the split lines, subtracting the length of the current line from the number
Maximum Length of Repeated Subarray (leetcode)
I’m looking at this leetcode question, and am having an issue completing the naive approach. I was able to come to an optimal solution here. But I’m not sure what’s wrong with my naive attempt. The question is as follows: Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example:
My check for whether a graph is a Binary Tree always returns false
I have this question that is medium level and couldn’t even think on how to solve this problem, my solution could be overkill as I have no idea on how to traverse a bunch of numbers in an array to check whether it is a binary tree or not. The program always returns false no matter what If you have
Find difference between two strings in JavaScript
I need to find difference between two strings. The expected output is to find the extra n and log it to the console. Is there any way to do this in JavaScript? Answer Another option, for more sophisticated difference checking, is to make use of the PatienceDiff algorithm. I ported this algorithm to Javascript at… https://github.com/jonTrent/PatienceDiff …which although the algorithm