Merge Two Binary Search Tree in Kotlin

To merge two binary search trees, you can perform an in-order traversal on both trees and merge the resulting sorted arrays. Then, you can construct a new binary search tree from the sorted array using a modified version of the binary search tree creation algorithm. Here’s the general algorithm to merge two binary search trees: … Read more

Flattening a Linked List in Kotlin

“Flattening” a linked list refers to the process of converting a nested linked list into a single-level linked list. A nested linked list is a linked list where the nodes can contain references to other linked lists. For example, consider the following nested linked list: 1 -> 2 -> 3 -> 4 -> 5 | … Read more

Merge K Sorted Linked List in Kotlin

To merge K sorted linked lists, you can use the “merge K lists” algorithm, which is a variant of the merge sort algorithm. Here’s an algorithm to merge K sorted linked lists: And here’s an algorithm to merge two sorted linked lists: And here’s an implementation of the “merge K lists” algorithm in Kotlin: You … Read more

Quick Sort on Linked List

Implementing quicksort on a linked list is a bit tricky as compared to an array because we can’t easily access elements by index. In quicksort, we need to partition the list into two sub-lists around a pivot element and then recursively sort those sub-lists. Here’s how we can implement quicksort on a linked list: In … Read more

Median of a Sorted Array in Kotlin

If the array has an odd number of elements, then the median is simply the middle element of the sorted array. If the array has an even number of elements, then the median is the average of the two middle elements of the sorted array. Here is a sample Kotlin code to find the median … Read more