将3个链表合并为1(Java)

Dan*_*ers 7 java algorithm merge sortedlist

我有一个问题需要对我正在进行的编码课程进行最终审核.它要求将3个链表合并为1个链表.我遇到的问题是合并列表时我能够按升序合并三个列表,但我错过了第二个列表23和25的最后2个节点.我无法弄清楚为什么它会停在那里.问题在于:

编写一个名为LinkedTest的程序:

  1. 创建三个已排序的单个链接的整数列表,如下所示

(第一份清单)2 11 19 21 24

(第二份清单)14 15 18 23 25

(第三份清单)3 9 17 20 22

  1. 将三个链接列表合并到一个新的已排序链接列表中,如下所示:2 3 9 11 14 15 17 18 19 20 21 22 23 24 25
  2. 返回新的已排序链接列表要求:您的程序必须具有小于或等于O的时间复杂度(nlog n)

这是我的代码:

public class LinkedTest {

public static class ListNode {

    private int data;
    ListNode next;

    public ListNode(int data) {
        this.data = data;
        next = null;
    }
}

ListNode head;

public static void main(String[] args) {

    LinkedTest list = new LinkedTest();

        int[] data1 = { 2, 11, 19, 21, 24 };

        ListNode head1 = new ListNode(data1[0]);

        for (int i = 1; i < data1.length; i++)
            list.push(head1, data1[i]);

       System.out.print("First List: ");
        list.display(head1);

        int[] data2 = { 14, 15, 18, 23, 25 };

        ListNode head2 = new ListNode(data2[0]);

        for (int count = 1; count < data2.length; count++)
            list.push(head2, data2[count]);

       System.out.println(" Second List: ") ;

        list.display(head2);

        int[] data3 = { 3, 9, 17, 20, 22 };

        ListNode head3 = new ListNode(data3[0]);

        for (int count = 1; count < data3.length; count++)
            list.push(head3, data3[count]);

       System.out.println(" Third List: ") ;

        list.display(head3);

        ListNode n = list.LinkedTest(head1, head2, head3);

        System.out.print(" Merged List: ");

        list.display(n);
    }



public ListNode LinkedTest(ListNode first, ListNode second, ListNode third) { 
      ListNode head = null;

        if (first == null && second != null && third != null)

            return second;

        else if (second == null && third != null && first != null)

            return third;

        else if (third == null && first != null && second != null)

            return first;

        else if (first.data < second.data && first.data < third.data) 
        {
            head = first;
            head.next = LinkedTest(first.next, second, third);
        } 
        else if (second.data < third.data && second.data < first.data)
        {
            head = second;
            head.next = LinkedTest(first, second.next, third);
        }

        else if (third.data < first.data && third.data < second.data)
        {
            head = third;
            head.next = LinkedTest(first, second, third.next);
        }

        return head;
    }

    public void push(ListNode head, int n) 
    {
        while (head.next != null)
            head = head.next;
        head.next = new ListNode(n);
    }

    public void display(ListNode head)
    {
        ListNode tempDisplay = head; 
        while (tempDisplay != null) 
        {
            System.out.print(tempDisplay.data);
            tempDisplay = tempDisplay.next; 
    }

    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

第一份清单:211192124第二份清单:1415182325第三份清单:39172022合并清单:23911141517181920212224

Jac*_* G. 0

您将获得三个排序列表,并被要求将它们合并为一个排序列表。实现这一点的算法非常简单。

我建议跟踪三个索引(每个输入列表一个)并将它们全部初始化为(每个列表0的第一个元素)。由于每个列表包含相同数量的元素,因此您可以简单地从to进行迭代,其中是其中一个列表的大小。03nn

在循环内部,您希望使用各自的索引找到每个列表的3 头元素之间的最小元素。为此,只需查看这些元素并将它们相互比较即可。找到最小值后,您可以将该元素附加到合并列表中,并增加相应列表的索引(这一点很重要,因为不要两次附加相同的元素)。

您将重复此3n操作(对三个列表中的每个元素重复一次),结果将是按升序排序的单个合并列表。

假设每个输入列表的大小始终相等,该算法O(3n)被简化为O(n)。

伪代码解决方案可能如下所示:

list1 = [2, 11, 19, 21, 24]
list2 = [14, 15, 18, 23, 25]
list3 = [3, 9, 17, 20, 22]
merged_list = []

indexes = [0, 0, 0]

for i in 0..15:
    minValue = Integer.MAX_VALUE
    minIndex = -1

    if indexes[0] < len(list1) and list1[indexes[0]] < minValue:
        minValue = list1[indexes[0]]
        minIndex = 0

    if indexes[1] < len(list2) and list2[indexes[1]] < minValue:
        minValue = list2[indexes[1]]
        minIndex = 1

    if indexes[2] < len(list3) and list3[indexes[2]] < minValue:
        minValue = list3[indexes[2]]
        minIndex = 2

    merged_list += minValue
    indexes[minIndex]++
Run Code Online (Sandbox Code Playgroud)