Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list’s nodes, only nodes itself may be changed. Example: Given 1->2->3->4, you should return the list as 2->1->4->3. Solution 1: public class Solution { public ListNode swapPairs(ListNode head) { if(head == null || head.next == null) return…… Continue reading Leetcode 24 Swap Nodes in Pairs Java Solution
Tag: Algorithms
Leetcode 21 Merge Two Sorted Lists Java Solution
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
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 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
Trapping Rain Water Problem LeetCode Java Solution
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 class Solution { public int trap(int[] height) { int arrLength = height.length; int total = 0; if(arrLength<=2) return total; int[] leftMaxArr = new int[arrLength]; int[]…… Continue reading Trapping Rain Water Problem LeetCode Java Solution