"Reversing a linked list is a very popular question. We have two approaches to reverse the linked list: Iterative approach and recursion approach.
Iterative approach (JavaScript)
function reverseLL(head){
if(head === null) return head;
let prv = null;
let next = null;
let cur = head;
while(cur){
next = cur.next; //backup
cur.next = prv;
prv = cur;
cur = next;
}
head = prv;
return head;
}
Recursion Approach (JS)
function reverseLLByRecursion("
Satyam S. - "Reversing a linked list is a very popular question. We have two approaches to reverse the linked list: Iterative approach and recursion approach.
Iterative approach (JavaScript)
function reverseLL(head){
if(head === null) return head;
let prv = null;
let next = null;
let cur = head;
while(cur){
next = cur.next; //backup
cur.next = prv;
prv = cur;
cur = next;
}
head = prv;
return head;
}
Recursion Approach (JS)
function reverseLLByRecursion("See full answer
"Since the problem asks for a O(logN) solution, I have to assume that the numbers are already sorted, meaning the same number are adjacent to each other, the value of the numbers shouldn't matter, and they expect us to use Binary Search.
First, we should analyze the pattern of a regular number array without a single disrupter.
Index: 0 1 2 3 4. 5 6. 7. 8. 9
Array:[1, 1, 2, 2, 4, 4, 5, 5, 6, 6]
notice the odd indexes are always referencing the second of the reoccurring numbers and t"
Bamboo Y. - "Since the problem asks for a O(logN) solution, I have to assume that the numbers are already sorted, meaning the same number are adjacent to each other, the value of the numbers shouldn't matter, and they expect us to use Binary Search.
First, we should analyze the pattern of a regular number array without a single disrupter.
Index: 0 1 2 3 4. 5 6. 7. 8. 9
Array:[1, 1, 2, 2, 4, 4, 5, 5, 6, 6]
notice the odd indexes are always referencing the second of the reoccurring numbers and t"See full answer
"Used Recursive approach to traverse the binary search tree and sum the values of the nodes that fall within the specified range [low, high]"
Srikant V. - "Used Recursive approach to traverse the binary search tree and sum the values of the nodes that fall within the specified range [low, high]"See full answer
"Implemented a recursive function which returns the length of the list so far. when the returned value equals k + 1 , assign current.next = current.next.next. If I made it back to the head return root.next as the new head of the linked list."
דניאל ר. - "Implemented a recursive function which returns the length of the list so far. when the returned value equals k + 1 , assign current.next = current.next.next. If I made it back to the head return root.next as the new head of the linked list."See full answer
"Traverse the array of points while computing the distance and pushing it to the heap. Then traverse again the heap and pop the k closest. Time O(nlogn) Space O(n)"
Dadja Z. - "Traverse the array of points while computing the distance and pushing it to the heap. Then traverse again the heap and pop the k closest. Time O(nlogn) Space O(n)"See full answer
"A recursive backtracking solution in python.
def changeSigns(nums: List[int], S: int) -> int:
res = []
n = len(nums)
def backtrack(index, curr, arr):
if curr == S and len(arr) == n:
res.append(arr[:])
return
if index >= len(nums):
return
for i in range(index, n):
add +ve number
arr.append(nums[i])
backtrack(i+1, curr + nums[i], arr)
arr.pop()
"
Yugaank K. - "A recursive backtracking solution in python.
def changeSigns(nums: List[int], S: int) -> int:
res = []
n = len(nums)
def backtrack(index, curr, arr):
if curr == S and len(arr) == n:
res.append(arr[:])
return
if index >= len(nums):
return
for i in range(index, n):
add +ve number
arr.append(nums[i])
backtrack(i+1, curr + nums[i], arr)
arr.pop()
"See full answer
"public class CircularBuffer {
private T[] buffer;
private int head;
private int tail;
private int size;
private final int capacity;
public CircularBuffer(int capacity) {
this.capacity = capacity;
this.buffer = (T[]) new Object[capacity];
this.head = 0;
this.tail = 0;
this.size = 0;
}
public void enqueue(T item) {
if (isFull()) {
throw new IllegalStateException("Buffer is full");
}
buf"
Vidhyadhar V. - "public class CircularBuffer {
private T[] buffer;
private int head;
private int tail;
private int size;
private final int capacity;
public CircularBuffer(int capacity) {
this.capacity = capacity;
this.buffer = (T[]) new Object[capacity];
this.head = 0;
this.tail = 0;
this.size = 0;
}
public void enqueue(T item) {
if (isFull()) {
throw new IllegalStateException("Buffer is full");
}
buf"See full answer
"Initialize left pointer: Set a left pointer left to 0.
Iterate through the array: Iterate through the array from left to right.
If the current element is not 0, swap it with the element at the left pointer and increment left.
Time complexity: O(n). The loop iterates through the entire array once, making it linear time.
Space complexity: O(1). The algorithm operates in-place, modifying the input array directly without using additional data structures.
"
Avon T. - "Initialize left pointer: Set a left pointer left to 0.
Iterate through the array: Iterate through the array from left to right.
If the current element is not 0, swap it with the element at the left pointer and increment left.
Time complexity: O(n). The loop iterates through the entire array once, making it linear time.
Space complexity: O(1). The algorithm operates in-place, modifying the input array directly without using additional data structures.
"See full answer
"const ops = {
'+': (a, b) => a+b,
'-': (a, b) => a-b,
'/': (a, b) => a/b,
'': (a, b) => ab,
};
function calc(expr) {
// Search for + or -
for (let i=expr.length-1; i >= 0; i--) {
const char = expr.charAt(i);
if (['+', '-'].includes(char)) {
return opschar), calc(expr.slice(i+1)));
}
}
// Search for / or *
for (let i=expr.length-1; i >= 0; i--) {
const char = expr.charAt(i);
if"
Tiago R. - "const ops = {
'+': (a, b) => a+b,
'-': (a, b) => a-b,
'/': (a, b) => a/b,
'': (a, b) => ab,
};
function calc(expr) {
// Search for + or -
for (let i=expr.length-1; i >= 0; i--) {
const char = expr.charAt(i);
if (['+', '-'].includes(char)) {
return opschar), calc(expr.slice(i+1)));
}
}
// Search for / or *
for (let i=expr.length-1; i >= 0; i--) {
const char = expr.charAt(i);
if"See full answer
"Arrays.sort(inputarray)
sliding window with a size of 2.
Check for the sum in the sliding window.
subtract the start when window moves"
Sridhar R. - "Arrays.sort(inputarray)
sliding window with a size of 2.
Check for the sum in the sliding window.
subtract the start when window moves"See full answer
"A much better solution than the one in the article, below:
It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods.
shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns.
My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N))
class ListNode {
constructor(val = 0, next = null) {
th"
Guilherme F. - "A much better solution than the one in the article, below:
It looks like the ones writing articles here in Javascript do not understand the time/space complexity of javascript methods.
shift, splice, sort, etc... In the solution article you have a shift and a sort being done inside a while, that is, the multiplication of Ns.
My solution, below, iterates through the list once and then sorts it, separately. It´s O(N+Log(N))
class ListNode {
constructor(val = 0, next = null) {
th"See full answer
"Implemented the Java code to find the largest island. It is similar to count the island. But in this we need to keep track of max island and compute its perimeter."
Techzen I. - "Implemented the Java code to find the largest island. It is similar to count the island. But in this we need to keep track of max island and compute its perimeter."See full answer
"bool isValidBST(TreeNode* root, long min = LONGMIN, long max = LONGMAX){
if (root == NULL)
return true;
if (root->val val >= max)
return false;
return isValidBST(root->left, min, root->val) &&
isValidBST(root->right, root->val, max);
}
`"
Alvaro R. - "bool isValidBST(TreeNode* root, long min = LONGMIN, long max = LONGMAX){
if (root == NULL)
return true;
if (root->val val >= max)
return false;
return isValidBST(root->left, min, root->val) &&
isValidBST(root->right, root->val, max);
}
`"See full answer
"This could be done using two-pointer approach assuming array is sorted: left and right pointers. We need track two sums (left and right) as we move pointers. For moving pointers we will move left to right by 1 (increment) when right sum is greater. We will move right pointer to left by 1 (decrement) when left sum is greater. at some point we will either get the sum same and that's when we exit from the loop. 0-left will be one array and right-(n-1) will be another array.
We are not going to mo"
Bhaskar B. - "This could be done using two-pointer approach assuming array is sorted: left and right pointers. We need track two sums (left and right) as we move pointers. For moving pointers we will move left to right by 1 (increment) when right sum is greater. We will move right pointer to left by 1 (decrement) when left sum is greater. at some point we will either get the sum same and that's when we exit from the loop. 0-left will be one array and right-(n-1) will be another array.
We are not going to mo"See full answer