Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 Solution 1: public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode p1 = l1; ListNode p2 = l2; ListNode…… Continue reading Leetcode 21 Merge Two Sorted Lists Java Solution
Category: Leetcode
Leetcode 20 Valid Parentheses Java Solution
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets.Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1: Input: “()” Output: true Example 2:…… Continue reading Leetcode 20 Valid Parentheses Java Solution
Leetcode 19 Remove Nth Node From End of List Java Solution
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Solution 1: public class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { if(head == null) return…… Continue reading Leetcode 19 Remove Nth Node From End of List Java Solution
Leetcode 15 3 Sum
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Solution 1:…… Continue reading Leetcode 15 3 Sum
Leetcode 5 Longest Palindromic Substring Java Solution
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: “babad” Output: “bab” Note: “aba” is also a valid answer. Example 2: Input: “cbbd” Output: “bb” Solution 1: class Solution { public String longestPalindrome(String s) { if(s==null || s.length()<=1) return s; boolean dp[][] =…… Continue reading Leetcode 5 Longest Palindromic Substring Java Solution
Leetcode 4 Median of Two Sorted Arrays Java Solution
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3,…… Continue reading Leetcode 4 Median of Two Sorted Arrays Java Solution
Leetcode 3 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Example 1: Input: “abcabcbb” Output: 3 Explanation: The answer is “abc”, with the length of 3. Example 2: Input: “bbbbb” Output: 1 Explanation: The answer is “b”, with the length of 1. Example 3: Input: “pwwkew” Output: 3 Explanation: The answer is “wke”, with…… Continue reading Leetcode 3 Longest Substring Without Repeating Characters
Leetcode 2 Add Two Numbers Java Solution
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example: Input: (2 ->…… Continue reading Leetcode 2 Add Two Numbers Java Solution
Leetcode 1 Two Sum Java Solution
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,…… Continue reading Leetcode 1 Two Sum Java Solution
Leetcode 406 Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of this person who have a height greater than or equal to h. Write an algorithm to reconstruct the queue. Note:The…… Continue reading Leetcode 406 Queue Reconstruction by Height