diff --git a/Linked list cycle.java b/Linked list cycle.java new file mode 100644 index 0000000..629d829 --- /dev/null +++ b/Linked list cycle.java @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution { + public boolean hasCycle(ListNode head) { + if (head == null) { + return false; + } + ListNode slow = head; + ListNode fast = head.next; + while (slow != fast) { + if (fast == null || fast.next == null) { + return false; + } + slow = slow.next; + fast = fast.next.next; + } + return true; + } +} diff --git a/Longest increasing subsequence.java b/Longest increasing subsequence.java new file mode 100644 index 0000000..1a3703d --- /dev/null +++ b/Longest increasing subsequence.java @@ -0,0 +1,21 @@ +class Solution { + public int lengthOfLIS(int[] nums) { + if(nums==null || nums.length==0) + return 0; + + int[] max = new int[nums.length]; + Arrays.fill(max, 1); + + int result = 1; + for(int i=0; inums[j]){ + max[i]= Math.max(max[i], max[j]+1); + + } + } + result = Math.max(max[i], result); + } + + return result; + }} diff --git a/counting bits.java b/counting bits.java new file mode 100644 index 0000000..5d226f0 --- /dev/null +++ b/counting bits.java @@ -0,0 +1,18 @@ +class Solution { + public int[] countBits(int n) { + int[] dp = new int[n+1]; + + dp[0] = 0; + int nearest = 0; + for (int k = 1; k <= n; k++) { + if ((k & (k-1)) == 0) { + dp[k] = 1; + nearest = k; + } else { + dp[k] = dp[k-nearest] + dp[nearest]; + } + } + + return dp; + } +} diff --git a/invert binary tree.java b/invert binary tree.java new file mode 100644 index 0000000..7abbf22 --- /dev/null +++ b/invert binary tree.java @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public TreeNode invertTree(TreeNode root) { + if(root==null){ + return null; + } + TreeNode right = invertTree(root.right); + TreeNode left = invertTree(root.left); + root.left = right; + root.right = left; + return root; + } +} diff --git a/kth smallest element in a bst.java b/kth smallest element in a bst.java new file mode 100644 index 0000000..f8bbdc5 --- /dev/null +++ b/kth smallest element in a bst.java @@ -0,0 +1,66 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + class Result + { + // Record how many nodes we visited in-order-wise so far; + // if the value is k then we know we've found the kth + // smallest and no need to do further recursion. + public Integer leftCount; + // null if not kth smallest; nonnull if we hit kth smallest + public Integer value; + + public Result(Integer leftCount, Integer value) + { + this.leftCount = leftCount; + this.value = value; + } + } + public int kthSmallest(TreeNode root, int k) { + // inorder DFS + return helper(root,k).value; + } + + private Result helper(TreeNode n, int k) + { + if (n==null) + { + return new Result(0,null); + } + + Result left = helper(n.left, k); + Result right = null; + if (left.leftCount==k) // kth smallest node is in left tree + { + return left; + } + else if (left.leftCount+1==k) // current root is our answer + { + return new Result(k,n.val); + } + else if (left.leftCount+1max) + max=s; + System.out.println(max); + i++; + } + System.out.println(max); + return max; + } +} diff --git a/merge k sorted lists.java b/merge k sorted lists.java new file mode 100644 index 0000000..f0da5ef --- /dev/null +++ b/merge k sorted lists.java @@ -0,0 +1,65 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode mergeKLists(ListNode[] lists) { + // Base condition + if (lists == null || lists.length == 0) { + return null; + } + return mergeKLists(lists, 0, lists.length - 1); + } + + private ListNode mergeKLists(ListNode[] lists, int start, int end) { + if (start == end) { + return lists[start]; + } + // Mid of list of lists + int mid = start + (end - start) / 2; + // Recursive call for left sublist + ListNode left = mergeKLists(lists, start, mid); + // Recursive call for right sublist + ListNode right = mergeKLists(lists, mid + 1, end); + // Merge the left and right sublist + return merge(left, right); + } + + private ListNode merge(ListNode left, ListNode right) { + // Create a dummy node + ListNode head = new ListNode(-1); + // Temp node + ListNode temp = head; + // Loop until any of the list becomes null + while (left != null && right != null) { + // Choose the value from the left and right which is smaller + if (left.val < right.val) { + temp.next = left; + left = left.next; + } else { + temp.next = right; + right = right.next; + } + temp = temp.next; + } + // Take all nodes from left list if remaining + while (left != null) { + temp.next = left; + left = left.next; + temp = temp.next; + } + // Take all nodes from right list if remaining + while (right != null) { + temp.next = right; + right = right.next; + temp = temp.next; + } + return head.next; + } +} diff --git a/number of 1 bits.java b/number of 1 bits.java new file mode 100644 index 0000000..60ee77c --- /dev/null +++ b/number of 1 bits.java @@ -0,0 +1,15 @@ +public class Solution { + // you need to treat n as an unsigned value + public int hammingWeight(int n) { + int count = 0; + for(int i=1; i<33; i++){ + if(getBit(n, i) == true){ + count++; + } + } + return count; +} + +public boolean getBit(int n, int i){ + return (n & (1 << i)) != 0; +}} diff --git a/remove nth node from end of list.java b/remove nth node from end of list.java new file mode 100644 index 0000000..3820faa --- /dev/null +++ b/remove nth node from end of list.java @@ -0,0 +1,23 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + ListNode fast = head, slow = head; + for (int i = 0; i < n; i++) fast = fast.next; + if (fast == null) return head.next; + while (fast.next != null) { + fast = fast.next; + slow = slow.next; + } + slow.next = slow.next.next; + return head; + } +} diff --git a/reorder list.java b/reorder list.java new file mode 100644 index 0000000..ad011d3 --- /dev/null +++ b/reorder list.java @@ -0,0 +1,57 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public void reorderList(ListNode head) { + ListNode slow = head; + ListNode fast = head; + + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + + ListNode lastHead = null; + + if (slow != null) { + ListNode next = null; + ListNode prev = null; + ListNode current = slow.next; + + while (current != null) { + next = current.next; + current.next = prev; + prev = current; + current = next; + } + + slow.next = null; + lastHead = prev; + } + + if (lastHead != null) { + ListNode firstHead = head; + + ListNode firstHeadNext = null; + ListNode lastHeadNext = null; + + while (firstHead != null && lastHead != null) { + firstHeadNext = firstHead.next; + lastHeadNext = lastHead.next; + + firstHead.next = lastHead; + lastHead.next = firstHeadNext; + + firstHead = firstHeadNext; + lastHead = lastHeadNext; + } + } + } +} diff --git a/same tree.java b/same tree.java new file mode 100644 index 0000000..549f609 --- /dev/null +++ b/same tree.java @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean isSameTree(TreeNode p, TreeNode q) { + if (p == null && q == null) + return true; + if (p == null || q == null) + return false; + if (p.val != q.val) + return false; + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); + + } +} diff --git a/subtree of another tree.java b/subtree of another tree.java new file mode 100644 index 0000000..48e9038 --- /dev/null +++ b/subtree of another tree.java @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +public class Solution { + // https://leetcode.com/problems/subtree-of-another-tree/solution/ + HashSet < String > trees = new HashSet < > (); + public boolean isSubtree(TreeNode s, TreeNode t) { + String tree1 = preorder(s, true); + String tree2 = preorder(t, true); + return tree1.indexOf(tree2) >= 0; + } + public String preorder(TreeNode t, boolean left) { + if (t == null) { + if (left) + return "lnull"; + else + return "rnull"; + } + return "#"+t.val + " " +preorder(t.left, true)+" " +preorder(t.right, false); + }} diff --git a/validate binary search tree.java b/validate binary search tree.java new file mode 100644 index 0000000..13ec87f --- /dev/null +++ b/validate binary search tree.java @@ -0,0 +1,38 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public boolean isValidBST(TreeNode root) { + return helper(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); +} + +public boolean helper(TreeNode root, double min, double max){ + if(root==null){ + return true; + } + + if(root.val<=min||root.val>=max){ + return false; + } + + boolean isLeftBST = helper(root.left, min, root.val); + boolean isRightBST = helper(root.right, root.val, max); + + if(!isLeftBST||!isRightBST){ + return false; + } + + return true; +}}