Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Given linked list – head = [4,5,1,9], which looks like following: Example 1: 1 2 3 Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. Example 2:...

<span title='2020-06-03 00:00:00 +0000 UTC'>June 3, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. Example: 1 2 Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this....

<span title='2020-05-30 00:00:00 +0000 UTC'>May 30, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Find the Town Judge

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: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. 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....

<span title='2020-05-29 00:00:00 +0000 UTC'>May 29, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Valid perfect sqaure

Given a positive integer num, write a function which returns True if num is a perfect square else False. Follow up: Do not use any built-in library function such as sqrt Example 1: 1 2 Input: num = 16 Output: true Example 2: 1 2 Input: num = 14 Output: false Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 class Solution { public boolean isPerfectSquare(int num) { return isPerfectSquareSearch(num); } public boolean isPerfectSquareNewTon(int num) { if (num < 2) return true; int root = num; while (root - num / root > 0) { root = (root + num/root) / 2; System....

<span title='2020-05-29 00:00:00 +0000 UTC'>May 29, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Check If It Is a Straight Line

You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane. Example 1: 1 2 Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]] Output: true Example 2: 1 2 Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]] Output: false Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public boolean checkStraightLine(int[][] coordinates) { // x2 - x1 int xdiff = coordinates[1][0] - coordinates[0][0]; // y2 - y1 int ydiff = coordinates[1][1] - coordinates[0][1]; for (int i = 2; i < coordinates....

<span title='2020-05-28 00:00:00 +0000 UTC'>May 28, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police....

<span title='2020-05-27 00:00:00 +0000 UTC'>May 27, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Squares of a Sorted Array

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: 1 2 Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] Example 2: 1 2 Input: [-7,-3,2,3,11] Output: [4,9,9,49,121] Note: 1 2 3 1 <= A.length <= 10000 -10000 <= A[i] <= 10000 A is sorted in non-decreasing order. Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 /** * Forward declaration of guess API....

<span title='2020-05-27 00:00:00 +0000 UTC'>May 27, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: 1 2 3 4 5 6 7 8 9 Given the sorted array: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: 0 / \ -3 9 / / -10 5 Solution:...

<span title='2020-05-26 00:00:00 +0000 UTC'>May 26, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Cousins in Binary Tree

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I’ll tell you whether the number is higher or lower. You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0): 1 2 3 -1 : My number is lower 1 : My number is higher 0 : Congrats!...

<span title='2020-05-26 00:00:00 +0000 UTC'>May 26, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Complement of Base 10 Integer

Every non-negative integer N has a binary representation. For example, 5 can be represented as “101” in binary, 11 as “1011” in binary, and so on. Note that except for N = 0, there are no leading zeroes in any binary representation. The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1. For example, the complement of “101” in binary is “010” in binary....

<span title='2020-05-25 00:00:00 +0000 UTC'>May 25, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

Cousins in Binary Tree

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree. Return true if and only if the nodes corresponding to the values x and y are cousins....

<span title='2020-05-25 00:00:00 +0000 UTC'>May 25, 2020</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;volyx

First Unique Character in a String

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1. Examples: 1 2 3 4 5 s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters. Solution: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public int firstUniqChar(String s) { Map<Character, Integer> freq = new HashMap<>(); for (int i = 0; i < s....

<span title='2020-05-25 00:00:00 +0000 UTC'>May 25, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Majority Element

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Example 1: 1 2 Input: [3,2,3] Output: 3 Example 2: 1 2 Input: [2,2,1,1,1,2,2] Output: 2 Solution: 1 2 3 4 5 6 class Solution { public int majorityElement(int[] nums) { Arrays....

<span title='2020-05-25 00:00:00 +0000 UTC'>May 25, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

Guess Number Higher or Lower

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the magazine string can only be used once in your ransom note. Example 1: 1 2 Input: ransomNote = "a", magazine = "b" Output: false Example 2: 1 2 Input: ransomNote = "aa", magazine = "ab" Output: false Example 3:...

<span title='2020-05-22 00:00:00 +0000 UTC'>May 22, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx

First Bad Version

You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels. The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so “a” is considered a different type of stone from “A”....

<span title='2020-05-20 00:00:00 +0000 UTC'>May 20, 2020</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;volyx