LeetCode – Plus One Linked List (Java)

Given a non-negative number represented as a singly linked list of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Example: Input: 1->2->3 Output: 1->2->4 Java Solution public ListNode plusOne(ListNode head) { ListNode h2 = reverse(head);   ListNode p=h2;   while(p!=null){ … Read more

LeetCode – Design Phone Directory (Java)

Design a Phone Directory which supports the following operations: get: Provide a number which is not assigned to anyone. check: Check if a number is available or not. release: Recycle or release a number. Java Solution 1 public class PhoneDirectory { int max; HashSet<Integer> set; LinkedList<Integer> queue;   /** Initialize your data structure here @param … Read more

LeetCode – Shuffle an Array (Java)

Shuffle a set of numbers without duplicates. Java Solution How we make sure each the probability of each element get shuffled is very similar to the streaming random problem. The algorithm is straightforward to understand, but the question is why it works. To have a working shuffle algorithm, every element in the array results in … Read more

LeetCode – Linked List Random Node (Java)

Given a singly linked list, return a random node’s value from the linked list. Each node must have the same probability of being chosen. Follow up: What if the linked list is extremely large and its length is unknown to you? Could you solve this efficiently without using extra space? Java Solution This problem is … Read more

LeetCode – Insert Delete GetRandom O(1) – Duplicates allowed (Java)

Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the collection. remove(val): Removes an item val from the collection if present. getRandom(): Returns a random element from current collection of elements. The probability of each element being returned is linearly … Read more

LeetCode – Insert Delete GetRandom O(1) (Java)

Design a data structure that supports all following operations in O(1) time. insert(val): Inserts an item val to the set if not already present. remove(val): Removes an item val from the set if present. getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned. Java … Read more

LeetCode – Shortest Word Distance (Java)

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. For example, Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”]. Given word1 = “coding”, word2 = “practice”, return 3. Given word1 = “makes”, word2 = “coding”, return 1. Java Solution public int … Read more

LeetCode – Largest BST Subtree (Java)

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Java Solution class Wrapper{ int size; int lower, upper; boolean isBST;   public Wrapper(){ lower = Integer.MAX_VALUE; upper = Integer.MIN_VALUE; isBST = false; size = 0; } } public … Read more

LeetCode – Combination Sum IV (Java)

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Java Solution This problem is similar to Coin Change. It’s a typical dynamic programming problem. public int combinationSum4(int[] nums, int target) { if(nums==null || nums.length==0) return 0;   int[] dp … Read more