Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Solution: For the solution, we take 2 nodes a “dummy” node and a “current” node. Use “dummy” node, in case if the head is needed to be deleted. “current” node is used to iterate throughout the list. So in …
Example 1: Input: nums = , key = 0 Output: true Before solving this problem, please look at the similar problem. Search in Rotated Sorted Array. We achieve this by below 2 lines: while (low < high && array == array) low++; // skip duplicates from …
Each element can appear only once or twice. Example 1: Given nums = The function should return 6, because . As each element can appear only twice. The solution is simple. We shall 2 variables, one will hold the new length and other is used to …
This problem is to place n number of queens on a chessboard such that, no two queens attack each other. Return all the solutions. Example: Input: 4 Output: , ] Introduction: A queen in a chess board can move it her left, right and diagonal. …
Given a 2D matrix and some words, return true of the letters are constructed with the combination cells that are horizontally or vertically adjacent to each other. Example: Matrix = , , ] Given word = "ABCCED", return true. Given word = "ABCD", return false. This …
Note: The solution set must not contain duplicate subsets. Example: Input: nums = Output: , , , , , , , ] The problem can be solved in 2 ways: Iterative Backtracking Solution 1: Iterative Solution. In this solution, …
Example: Input: n = 4, k = 2 Output: , , , , , , ] This problem can be solved in 2 ways. Iterative Recursion/Backtrack Solution 1: Iterative approach: In the iterative solution, we take a temp array of size “k”. Then we move to the …
We represent color red, white, and blue with integers 0, 1, and 2 to represent respectively. Example: Input: Output: We can solve this by many different ways. Solution 1 will be counting all the number of 0’s, 1’s and 2’s. Then insert those number of elements …
Example 1: Input: matrix = , , ] target = 3 Output: true This problem can be solved in 2 ways. By eliminating one row or column at a time. By binary search. 1. By eliminating one row or column at a time. We start …
Example 1: Input: , , ] Output: , , ] We shall solve this problem by using 2 solutions. Be sure to understand both the solutions, as solution 2 is depended on solution 1. In solution 1 we shall make use of …