【问题标题】:Recursive merge sort of two Linked Lists using third reference使用第三个引用对两个链表进行递归合并排序
【发布时间】:2015-06-05 17:36:36
【问题描述】:

我正在尝试使用第三个引用来解决递归合并排序。我想获取第三个参考,并继续按排序顺序链接两个链表(两个链表单独排序)中的节点,而不创建任何额外的节点。

我看到有一个递归程序引用了here。但是我想使用第三个参考来尝试这个,但我一直把它搞砸了。谁能让我知道我在这里缺少什么条件?

import java.util.ArrayList;
import java.util.ListIterator;

public class MergeLinkedListsIntoExisting {
    public static void main(String[] args){
        Node nodeList1 = null, nodeList2 = null;
        Node temp = null;
        ArrayList<Integer> array1 = new ArrayList<Integer>();
        array1.add(3);
        array1.add(7);
        array1.add(9);

        ArrayList<Integer> array2 = new ArrayList<Integer>();
        array2.add(1);
        array2.add(2);
        array2.add(8);

        nodeList1 = add(nodeList1, array1);
        nodeList2 = add(nodeList2, array2);
        System.out.println("**List 1**");
        print(nodeList1);
        System.out.println("**List 2**");
        print(nodeList2);
        System.out.println("Sorted List");
        Node nodeList3 = mergeTwoLists(nodeList1, nodeList2, temp);
        print(nodeList3);
    }
    private static Node add(Node node, ArrayList<Integer> list){
        Node current = node;
        Node head = node;
        ListIterator<Integer> it = list.listIterator();
        while(it.hasNext()){
            if(head==null){
                head = new Node();
                head.data = it.next();
                head.next=null;
                node = head;
            }
            else{
                current = new Node();
                current.data = it.next();
                current.next = null;
                node.next = current;
                node = node.next;
            }
        }
        return head;
    }

    private static void print(Node node) {
        if(node!=null){
            while(node.next!=null){
                System.out.print(node.data + " ");
                node = node.next;
            }
            System.out.println(node.data);
        }
        else{
            System.out.println("No elements in the linkedList.");
        }
    }


    private static Node mergeTwoLists(Node nodeList1, Node nodeList2, Node temp) {
        if(nodeList1 == null) return nodeList2;
        if(nodeList2 == null) return nodeList1;

        if(nodeList1.data <= nodeList2.data){
            if(temp == null){
                temp = nodeList1;
                temp.next = mergeTwoLists(nodeList1.next, nodeList2, temp.next);
            }
            else{
                System.out.println(temp.data);
                temp.next = mergeTwoLists(nodeList1.next, nodeList2, temp.next);
            }
        }else{
            if(temp == null){
                temp = nodeList2;
                System.out.println(temp.data);
                temp.next = mergeTwoLists(nodeList1, nodeList2.next, temp.next);
            }
            else{
                System.out.println(temp.data);
                temp.next = mergeTwoLists(nodeList1, nodeList2.next, temp.next);
            }
        }
        return temp;
    }
}

解析应该在mergeTwoLists的递归调用中使用temp.next。根据需要纠正我和我的方法。

【问题讨论】:

    标签: java sorting recursion


    【解决方案1】:

    如果你想要递归方法

    private static Node mergeTwoLists(Node nodeList1, Node nodeList2) {
        if(nodeList1 == null) return nodeList2;
        if(nodeList2 == null) return nodeList1;
        Node tmp = new Node();
        if(nodeList1.data <= nodeList2.data){
            tmp.data = nodeList1.data;
            tmp.next = mergeTwoLists(nodeList1.next, nodeList2);
        }else{
            tmp.data = nodeList2.data;
            tmp.next = mergeTwoLists(nodeList1, nodeList2.next);
        }
        return temp;
    }
    

    【讨论】:

    • 我想看看如何解决我的方法。如问题中提到的(超链接),有许多示例可以执行递归方式。我要检查的是我在程序中做的一团糟。无论如何,谢谢,不过你的方式也是一个不错的方法。
    【解决方案2】:

    编辑:我刚刚又看了一遍,我认为修复代码的主要修改只是删除了 temp 等于 null 的特殊情况。

    我刚刚修改了您的代码并使其正常工作。只需通过temp 而不是temp.next,如果 temp 为 null,则不需要特殊情况。

    import java.util.ArrayList;
    import java.util.ListIterator;
    
    public class MergeLinkedListsIntoExisting {
        public static void main(String[] args){
            Node nodeList1 = null, nodeList2 = null;
            Node temp = null;
            ArrayList<Integer> array1 = new ArrayList<Integer>();
            array1.add(3);
            array1.add(7);
            array1.add(9);
    
            ArrayList<Integer> array2 = new ArrayList<Integer>();
            array2.add(1);
            array2.add(2);
            array2.add(8);
    
            nodeList1 = add(nodeList1, array1);
            nodeList2 = add(nodeList2, array2);
            System.out.println("**List 1**");
            print(nodeList1);
            System.out.println("**List 2**");
            print(nodeList2);
            System.out.println("Sorted List");
            Node nodeList3 = mergeTwoLists(nodeList1, nodeList2, temp);
            print(nodeList3);
        }
        private static Node add(Node node, ArrayList<Integer> list){
            Node current = node;
            Node head = node;
            ListIterator<Integer> it = list.listIterator();
            while(it.hasNext()){
                if(head==null){
                    head = new Node();
                    head.data = it.next();
                    head.next=null;
                    node = head;
                }
                else{
                    current = new Node();
                    current.data = it.next();
                    current.next = null;
                    node.next = current;
                    node = node.next;
                }
            }
            return head;
        }
    
        private static void print(Node node) {
            if(node!=null){
                while(node.next!=null){
                    System.out.print(node.data + " ");
                    node = node.next;
                }
                System.out.println(node.data);
            }
            else{
                System.out.println("No elements in the linkedList.");
            }
        }
    
    
        private static Node mergeTwoLists(Node nodeList1, Node nodeList2, Node temp) {
            if(nodeList1 == null) return nodeList2;
            if(nodeList2 == null) return nodeList1;
    
            if(nodeList1.data <= nodeList2.data){
    
              temp = nodeList1;
              temp.next = mergeTwoLists(nodeList1.next, nodeList2, temp);
    
            }else{
    
              temp = nodeList2;
              temp.next = mergeTwoLists(nodeList1, nodeList2.next, temp);                
            }
            return temp;
        }
    }
    
    public class Node{
        int data;
        Node next;
    }
    

    【讨论】:

    • 为什么要交换参数? nodeList2.next 代替 nodeList1,反之亦然?但是有条件将它路由到正确的 if 分支,那我们为什么需要交换它呢?
    • @Deepak 我今天意识到实际上没有必要切换参数。我刚刚修改了答案。
    • 现在其他观众不会感到困惑了。感谢您的编辑!
    猜你喜欢
    • 1970-01-01
    • 2018-01-04
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多