LeetCode - Blind 75 - Merge Two Sorted Lists

The Problem You are given the heads of two sorted linked lists, list1 and list2. Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Examples Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Input: list1 = [], list2 = [] Output: [] Input: list1 = [], list2 = [0] Output: [0] Constraints The number of nodes in both lists is in the range [0, 50]. -100 <= Node.val <= 100 Both list1 and list2 are sorted in non-decreasing order. Recursive Solution func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? { guard let list1 = list1 else { return list2 } guard let list2 = list2 else { return list1 } if list1.val <= list2.val { list1.next = mergeTwoLists(list1.next, list2) return list1 } else { list2.next = mergeTwoLists(list1, list2.next) return list2 } } Explanation Before moving to the part where we use recursion, we need to handle the base case and check list1 and list2 for nil values. Then we compare the node values and move the next pointer accordingly. ...

December 20, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Reverse Linked List

The problem Given the head of a singly linked list, reverse the list, and return the reversed list. Examples Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Input: head = [1,2] Output: [2,1] Input: head = [] Output: [] Constraints The number of nodes in the list is in the range [0, 5000]. -5000 <= Node.val <= 5000 Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? ...

December 17, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Search in Rotated Sorted Array

The problem There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. You must write an algorithm with O(log n) runtime complexity. ...

December 11, 2024 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Find Minimum in Rotated Sorted Array

The Problem Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. Given the sorted rotated array nums of unique elements, return the minimum element of this array. You must write an algorithm that runs in O(log n) time. ...

December 9, 2024 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Valid Parentheses

The Problem Given a string s 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. Every closing bracket has a corresponding open bracket of the same type. Examples Input: s = "()" Output: true Input: s = "()[]{}" Output: true Input: s = "(]" Output: false Input: s = "([])" Output: true Constraints 1 <= s.length <= 10⁴ s consists of parentheses only '()[]{}'. Brute Force Solution func isValid(_ s: String) -> Bool { var s = s while s.contains("()") || s.contains("{}") || s.contains("[]") { s = s.replacingOccurrences(of: "()", with: "") s = s.replacingOccurrences(of: "{}", with: "") s = s.replacingOccurrences(of: "[]", with: "") } return s.isEmpty } Explanation The brute force way to solve this problem is to check if the string contains "()", "{}", "[]" and replace them with an empty string. It’s not a very efficient algorithm and takes O(n²) time because of the while loop and the replacing operation on the entire string. We can optimize it by using a stack data structure. ...

December 5, 2024 · 2 min · Dmytro Chumakov