简体   繁体   English

使用Java合并两个链接列表

[英]Merge Two Linked Lists using Java

Trying to figure out what I'm missing in my code that is supposed to merge linked list 2 to the end of linked list 1. Right now it's just getting the last element in the second list and returning that. 试图找出代码中我所缺失的内容,该内容应该将链表2合并到链表1的末尾。现在,它只是获取第二个列表中的最后一个元素并返回它。

The logic I was trying to use is walking down the first list (L1) and adding those elements one-by-one to new_list then doing the same for the second list (L2) after I've reached the end of L1. 我尝试使用的逻辑是沿着第一个列表(L1)向下移动,并将这些元素一个一个地添加到new_list中,然后在到达L1末尾后对第二个列表(L2)进行相同的操作。 I am also trying to avoid modifying L1 or L2, which is why I created a new_list. 我也试图避免修改L1或L2,这就是为什么我创建了一个new_list。

Any help would be greatly appreciated. 任何帮助将不胜感激。

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;
        }
    }
}

You need to retain a reference to the first node in your list, which you do not do. 您需要保留对列表中第一个节点的引用,而不必这样做。 In the example below, I also break up your loop into two loops with predetermined termination conditions, since that is logically what you are trying to do. 在下面的示例中,我还将您的循环分成两个具有预定终止条件的循环,因为从逻辑上讲,这是您要尝试的操作。 Note that I never copy a reference to the existing list's elements, since you mentioned that you never want to modify them. 请注意,我从未复制对现有列表元素的引用,因为您提到您永远不想修改它们。 I do however increment the local reference to the inputs: 但是,我确实增加了对输入的本地引用:

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;
}

As you can see, this has a lot of code repetition, so it can easily be generalized for an arbitrary number of lists: 如您所见,这具有大量的代码重复,因此可以轻松地将其概括为任意数量的列表:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM