I would like to share how I thought about the solution ... I saw a solution that includes recursion, and they are quite amazing, is the result of well-functional and modular thinking. I really appreciate sharing.
I would like to add that recursion will not work for large strings, stack calls will overflow; so I decided to try an iterative approach ... and this is what I get.
The code is pretty straightforward, I added some inline comments to try and assure this.
If you donβt do this, let me know and I will improve readability (maybe I have a misleading interpretation of my own code).
import java.util.Random; public class Solution { public static class Node<T extends Comparable<? super T>> implements Comparable<Node<T>> { T data; Node next; @Override public int compareTo(Node<T> otherNode) { return data.compareTo(otherNode.data); } @Override public String toString() { return ((data != null) ? data.toString() + ((next != null) ? "," + next.toString() : "") : "null"); } } public static Node merge(Node firstLeft, Node firstRight) { combine(firstLeft, firstRight); return Comparision.perform(firstLeft, firstRight).min; } private static void combine(Node leftNode, Node rightNode) { while (leftNode != null && rightNode != null) {
}
Victor Aug 29 '15 at 17:57 2015-08-29 17:57
source share