LeetCode - Blind 75 - Reorder List

The Problem You are given the head of a singly linked list. The list can be represented as: L0 → L1 → … → Ln - 1 → Ln Reorder the list to be in the following form: L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → … You may not modify the values in the list’s nodes. Only the nodes themselves may be changed. ...

December 23, 2024 · 3 min · Dmytro Chumakov

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