Merge K Sorted Lists

Problem You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Examples 1 -> 10 -> 20 4 -> 11 -> 13 3 -> 8 -> 9 will result in 1 -> 3 -> 4 -> 8 -> 9 -> 10 -> 11 -> 13 -> 20 ...

Merge Sort Algorithm

Problem Array Sorting Problem Similar Sorts 3-way Merge Sort Solution Mergesort is one of the older algorithms known since 1945. It is very good example of divide and conquer paradigm and is better than other simpler sort algorithms like selection, insertion or bubble sort. Method 1 - Recursive Top Down Merge Sort Merge sort is a recursive algorithm that repeatedly calls itself on progressively smaller subsets of the problem. ...

Merge Two Sorted Linked Lists

Problem Given two sorted Linked Lists, how to merge them into the third list in sorted order? Example Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Merged List: 1 -> 1 -> 2 -> 3 -> 4 -> 4 ...

Merge Sort in a Linked list

Problem Given a Linked List, Sort it using merge sort. Examples Example 1: Input: head = [4,2,1,3] Output: [1,2,3,4] Solution Method 1 - Recursive Refer merge sort - Merge Sort Algorithm. Get the pointer to middle node Split the list at the middle node Continue 1 and 2 , till list size is 1 or 2. Sort the elements in step 3 Merge the sorted list Now this involves 2 problems - Middle of the linked list Problem and Merge Two Sorted Linked Lists. To split linked list into 2, we can also use: Split Linked List into two halves fractionally given n ...

Merge Overlapping Intervals

Problem Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input. OR Given a list of intervals, merge all the overlapping intervals to produce a list that has only mutually exclusive intervals. Examples Example 1: Input: intervals = [1,3],[2,6],[8,10],[15,18], Output: [1,6],[8,10],[15,18]. ...

Merge Two Sorted Arrays

Problem Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. Similar Problem You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. ...

Given an array count max number of surpassers

Problem For an array of integers, a surpasser of an integer element is an element on its right hand side that is larger than it. Given an array of integers, Output the max number of surpassers. Examples Example 1: Input: nums = [10, 3, 4, 5, 2] Output: 2 Explanation: The surpassers of 3 is 4 and 5, and 10 doesn’t have any surpasser. ...

Merge a Linked list into another at Alternate Positions

Problem Given two linked lists, merge one list into another at alternate positions, if second link list has extra nodes, print them as well Examples Example 1: Input: list1 = [1, 2, 3], list2 = [4, 5, 6, 7, 8] Output: [1, 4, 2, 5, 3, 6] Remaining nodes from list2: [7, 8] Explanation: Merged list is formed by taking one element from each list alternately. The remaining elements of list2 are printed. ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy