Interview Question: Merge two sorted singly linked lists without creating new nodes

后端 未结 26 2951
有刺的猬
有刺的猬 2020-12-02 04:09

This is a programming question asked during a written test for an interview. \"You have two singly linked lists that are already sorted, you have to merge them and return a

26条回答
  •  既然无缘
    2020-12-02 04:23

    I would like to share how i thought the solution... i saw the solution that involves recursion and they are pretty amazing, is the outcome of well functional and modular thinking. I really appreciate the sharing.

    I would like to add that recursion won't work for big lits, the stack calls will overflow; so i decided to try the iterative approach... and this is what i get.

    The code is pretty self explanatory, i added some inline comments to try to assure this.

    If you don't get it, please notify me and i will improve the readability (perhaps i am having a misleading interpretation of my own code).

    import java.util.Random;
    
    
    public class Solution {
    
        public static class Node> implements Comparable> {
    
            T data;
            Node next;
    
            @Override
            public int compareTo(Node 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) {
                // get comparision data about "current pair of nodes being analized".
                Comparision comparision = Comparision.perform(leftNode, rightNode);
                // stores references to the next nodes
                Node nextLeft = leftNode.next; 
                Node nextRight = rightNode.next;
                // set the "next node" of the "minor node" between the "current pair of nodes being analized"...
                // ...to be equals the minor node between the "major node" and "the next one of the minor node" of the former comparision.
                comparision.min.next = Comparision.perform(comparision.max, comparision.min.next).min;
                if (comparision.min == leftNode) {
                    leftNode = nextLeft;
                } else {
                    rightNode = nextRight;
                }
            }
        }
    
    /** Stores references to two nodes viewed as one minimum and one maximum. The static factory method populates properly the instance being build */
        private static class Comparision {
    
            private final Node min;
            private final Node max;
    
            private Comparision(Node min, Node max) {
                this.min = min;
                this.max = max;
            }
    
            private static Comparision perform(Node a, Node b) {
                Node min, max;
                if (a != null && b != null) {
                    int comparision = a.compareTo(b);
                    if (comparision <= 0) {
                        min = a;
                        max = b;
                    } else {
                        min = b;
                        max = a;
                    }
                } else {
                    max = null;
                    min = (a != null) ? a : b;
                }
                return new Comparision(min, max);
            }
        }
    
    // Test example....
        public static void main(String args[]) {
            Node firstLeft = buildList(20);
            Node firstRight = buildList(40);
            Node firstBoth = merge(firstLeft, firstRight);
            System.out.println(firstBoth);
        }
    
    // someone need to write something like this i guess...
        public static Node buildList(int size) {
            Random r = new Random();
            Node first = new Node<>();
            first.data = 0;
            first.next = null;
            Node current = first;
            Integer last = first.data;
            for (int i = 1; i < size; i++) {
                Node node = new Node<>();
                node.data = last + r.nextInt(5);
                last = node.data;
                node.next = null;
                current.next = node;
                current = node;
            }
            return first;
        }
    

    }

提交回复
热议问题