# JavaScript Algorithm: How to Reverse Linked List

## Prerequisite: Singly Linked List in JavaScript

When given a singly linked list how can we manage to reverse it? Like the following example, if the input linked list is 1->2->3->4->5->NULL, can we reverse it into the output as 5->4->3->2->1->NULL?

# Problem

Given an array of integers `A` sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Example 1:

`Input: [-4,-1,0,3,10]Output: [0,1,9,16,100]`

Example 2:

`Input: [-7,-3,2,3,11]Output: [4,9,9,49,121]`

Note:

1. `1 <= A.length <= 10000`
2. `-10000 <= A[i] <= 10000`
3. `A` is sorted in non-decreasing order.

# Solution

Brute Force

First, loop through the given array, and return the squared element. …

# Problem

`In a town, there are N people labelled from 1 to N.  There is a rumor that one of these people is secretly the town judge.If the town judge exists, then:1. The town judge trusts nobody.2. Everybody (except for the town judge) trusts the town judge.3. There is exactly one person that satisfies properties 1 and 2.You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.If the town judge exists and can be identified, return the label of the town judge. …`

# How To Merge Two Binary Trees In JavaScript🌳

Prerequisite: Tree Traversal In JavaScript

Problem

`Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.`

Example input:

Example output merged tree:

Note: The merging process must start from the root nodes of both trees. …

# JavaScript Solution to Minimum Depth of Binary Tree

Prerequisite: Tree Traversal In JavaScript

For our last She’s coding data structure and algorithm event I chose minimum depth of binary tree problem to practice tree traversing with our participants. It can be challenging at first, with the help of depth first search and breadth first search we can solve it easily.

Problem

`Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Note: A leaf is a node with no children.Example 1:     3    / \   9  20       / \      15…`

What is Redux-Saga?

Redux-Saga, like Thunk, is a redux middleware for React/Redux application. With the help of Redux-Saga, the side effect becomes easier to manage, more efficient to execute, simple to test and better at handling the failures. It has access to the full redux application state and it can dispatch redux actions. Unlike thunk, its thread can be started, paused and canceled from the main application with normal redux actions.

How do we implement Redux-Saga?

To make the implementation easy to understand, I’ll use my basic React/Redux weather app, to check the 7 days weather forecast of the selected city with the help of One Call API. …

# JavaScript Solution to Maximum Depth of N-ary Tree

Prerequisite: Tree Traversal In JavaScript

For our last ‘She’s coding’ data structure and algorithm practice event, we chose to solve maximum depth of N-ary tree problem(link). We had interesting discussion about approaches to solve it. In the following blog, I’ll share the common solution to this problem.

# Problem:

`Given a n-ary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).`

Example…

# JavaScript Solution of Trapping Rain Water

For my last technical interview, I was asked about how to trap rainwater. (Not really 🤪). It was a hard level Leetcode problem. I found it challenging, that’s why I’d like to write a blog about it. To make sure I understand the approach to solve similar problems in the future.

# Problem:

`Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.` The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
`Example:Input: [0,1,0,2,1,0,1,3,2,1,2,1]Output: 6`

It seems like an unsolvable problem, don’t worry, we’ll “tackle” it together.

First, let’s see some key points to consider. The given array is non-negative integers. Thanks to this example image, the width of each bar that represents each element in this array is 1. We need to compute how much blue square the map can contain. Does this help? If not, let’s draw examples to make sure we understand the problem. …

# JavaScript Solution to Number of Recent Calls

Number of Recent Calls is one of the leetcode questions that tripped us up. (As you can see how many downvotes it has.🧐) In the following blog, I’ll try to explain the problem and the example line by line to make it easier to understand.

Explanation of the question

Write a class `RecentCounter` to count recent requests.

The key point above is class, we need to implement a class, not a function.

It has only one method: `ping(int t)`, where t represents some time in milliseconds.

The class instance needs to have ping method, which is a function that takes t, an integer in the range of `1 <= t <= 10^9` , as an argument.

# Cycle Detection of A Linked List in JavaScript

Cycle detection is the algorithmic problem of finding a cycle in a sequence of iterated function values, for example, the classic linked list loop detection problem. There are different solution. For the following Leetcode example, I’ll explain true solutions.

Given `head`, the head of a linked list, determine if the linked list has a cycle in it.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. … 