Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java合并两个链表_Java_Merge_Linked List - Fatal编程技术网

使用Java合并两个链表

使用Java合并两个链表,java,merge,linked-list,Java,Merge,Linked List,试图找出我应该将链表2合并到链表1末尾的代码中缺少的内容。现在它只是获取第二个列表中的最后一个元素并返回它 我尝试使用的逻辑是沿着第一个列表(L1)向下走,将这些元素一个接一个地添加到新的_列表中,然后在到达L1末尾后对第二个列表(L2)执行相同的操作。我还试图避免修改L1或L2,这就是为什么我创建了一个新的_列表 任何帮助都将不胜感激 public NodeList(int item, NodeList next) { this.item = item; this.next =

试图找出我应该将链表2合并到链表1末尾的代码中缺少的内容。现在它只是获取第二个列表中的最后一个元素并返回它

我尝试使用的逻辑是沿着第一个列表(L1)向下走,将这些元素一个接一个地添加到新的_列表中,然后在到达L1末尾后对第二个列表(L2)执行相同的操作。我还试图避免修改L1或L2,这就是为什么我创建了一个新的_列表

任何帮助都将不胜感激

public NodeList(int item, NodeList next) {
    this.item = item;
    this.next = next;
}

public static NodeList merge(NodeList l1, NodeList l2) {

    NodeList new_list = new NodeList(l1.item, l1.next);
    NodeList new_list2 = new NodeList(l2.item, l2.next);

    while (true) {
        if (new_list.next == null) {
            if (new_list2.next == null) {
                return new_list;
            }
            else {
                new_list.next = new NodeList(new_list2.next.item, new_list2.next.next);
                new_list2 = new_list2.next;
            }

        }
        else {
            new_list.next = new NodeList(new_list.next.item, new_list.next.next);
            new_list = new_list.next;
        }
    }
}

您需要保留对列表中第一个节点的引用,但您不需要这样做。在下面的示例中,我还将您的循环分解为两个具有预定终止条件的循环,因为这在逻辑上是您尝试执行的操作。请注意,我从不复制对现有列表元素的引用,因为您提到您永远不想修改它们。但是,我不增加输入的本地参考:

public static NodeList merge(NodeList l1, NodeList l2) {

    NodeList new_head = new NodeList(0, null);
    NodeList new_node = new_head;

    for(; l1 != null; l1 = l1.next) {
        new_node.next = new NodeList(l1.item, null);
        new_node = new_node.next;
    }

    for(; l2 != null; l2 = l2.next) {
        new_node.next = new NodeList(l2.item, null);
        new_node = new_node.next;
    }
    return new_head.next;
}
如您所见,这有很多代码重复,因此很容易将其推广到任意数量的列表:

public static NodeList merge(NodeList... l) {

    NodeList new_head = new NodeList(0, null);
    NodeList new_node = new_head;

    for(NodeList ln in l) {
        for(; ln != null; ln = ln.next) {
            new_node.next = new NodeList(ln.item, null);
            new_node = new_node.next;
        }
    }
    return new_head.next;
}

您需要保留对列表中第一个节点的引用,但您不需要这样做。在下面的示例中,我还将您的循环分解为两个具有预定终止条件的循环,因为这在逻辑上是您尝试执行的操作。请注意,我从不复制对现有列表元素的引用,因为您提到您永远不想修改它们。但是,我不增加输入的本地参考:

public static NodeList merge(NodeList l1, NodeList l2) {

    NodeList new_head = new NodeList(0, null);
    NodeList new_node = new_head;

    for(; l1 != null; l1 = l1.next) {
        new_node.next = new NodeList(l1.item, null);
        new_node = new_node.next;
    }

    for(; l2 != null; l2 = l2.next) {
        new_node.next = new NodeList(l2.item, null);
        new_node = new_node.next;
    }
    return new_head.next;
}
如您所见,这有很多代码重复,因此很容易将其推广到任意数量的列表:

public static NodeList merge(NodeList... l) {

    NodeList new_head = new NodeList(0, null);
    NodeList new_node = new_head;

    for(NodeList ln in l) {
        for(; ln != null; ln = ln.next) {
            new_node.next = new NodeList(ln.item, null);
            new_node = new_node.next;
        }
    }
    return new_head.next;
}

您的while循环永不终止似乎您在混合
Node
NodeList
概念。@SeanPatrickFloyd当两个列表都为空时,代码返回调用方法。@azurefrog啊,错过了
new\u list=new\u list.next是您的问题。定义一个
new\u head
引用,该引用从指向
new\u list
开始,并返回该引用。while循环永不终止似乎将
Node
NodeList
概念混在一起。@SeanPatrickFloyd当两个列表都为null时,代码返回调用方法。@azurefrog啊,错过了
new_list=new_list.next是您的问题。定义一个
new\u head
引用,开始指向
new\u list
并返回该引用。啊,谢谢!比我想做的要清楚得多。啊,谢谢!比我想做的要清楚得多。