This is No.148 Problem in LeetCode. Here are two approaches as below: Top Down Merge Sort Recursively split the original list into two halves. The split continues until there is only one node in the linked list (Divide phase). To split the list into two halves, we find the middle of the linked list using …
An example to overwrite comparator in method to customize sorting of array
I still feel confused when I want to customize the way of sorting for array and collections in java. And here is a good example of how to write a customized sorting function to sort string array: Comparator<String> comp = new Comparator<String>(){ @Override public int compare(String str1, String str2){ String s1 = str1 + str2; …
Continue reading “An example to overwrite comparator in method to customize sorting of array”
Sorting a HashMap according to keys in Java
Using TreeMap // Java Code to sort Map by key value import java.util.*; class sortmapKey { // This map stores unsorted values static Map<String, Integer> map = new HashMap<>(); // Function to sort map by Key public static void sortbykey() { // TreeMap to store values of HashMap TreeMap<String, Integer> sorted = new TreeMap<>(); // …
Continue reading “Sorting a HashMap according to keys in Java”