📌  相关文章
📜  Java程序合并新列表中的两个排序链表

📅  最后修改于: 2021-09-04 08:00:11             🧑  作者: Mango

我们得到了两个已排序的 List,我们的目标是将这两个列表合并为一个新列表。为此,我们必须编写一个函数,它将两个 List 作为参数,并按升序排序。此函数将这两个 List 按递增顺序合并为一个 List。

Input
List 1 : 1-> 3-> 4-> 9->10
List 2 : 2-> 5-> 6-> 9

Output
New List : 1-> 2-> 3-> 4-> 5-> 6-> 9-> 9-> 10

我们有两种方法来解决这个问题:

  1. 迭代
  2. 递归

方法一:迭代法

  • 这种方法背后的想法是我们将在新列表中多取一个节点,即列表的头节点。
  • 我们将采用列表类型的一个变量,该变量始终位于列表的最后一个节点,以便添加新节点变得更容易。
  • 我们将迭代循环并检查两个列表中较小的元素并将该节点附加到结果列表中。
  • 如果我们到达任何列表的末尾,那么我们将简单地附加第二个列表中的剩余节点。

执行:

Java
// Java Program to Merge Two Sorted
// Linked Lists in New List
// Iteratively
  
import java.io.*;
  
public class ListNode {
  
    int val;
    ListNode next;
  
    ListNode() {}
    ListNode(int val) { this.val = val; }
  
    ListNode(int val, ListNode next)
    {
        this.val = val;
        this.next = next;
    }
}
  
class GFG {
    public static ListNode mergeTwoLists(ListNode l1,
                                         ListNode l2)
    {
        // New List
        ListNode result = new ListNode(-1);
  
        // variable to point the last node of the list.
        ListNode p = result;
  
        // Iterate the loop
        while (l1 != null && l2 != null) {
            // Find the smaller element and append it to the
            // list.
            if (l1.val <= l2.val) {
                p.next = l1;
                l1 = l1.next;
            }
  
            else {
                p.next = l2;
                l2 = l2.next;
            }
  
            // Update the variable
            p = p.next;
        }
  
        // If anyone list become empty append the remaining
        // list element of othe list.
        if (l1 == null) {
            p.next = l2;
        }
  
        else if (l2 == null) {
            p.next = l1;
        }
  
        // Return the resultant list without first extra
        // node
        return result.next;
    }
  
    // A utility function to print linked list
    static void printList(ListNode node)
    {
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        ListNode head1 = new ListNode(1);
        head1.next = new ListNode(3);
        head1.next.next = new ListNode(5);
  
        // 1->3->5 LinkedList created
        ListNode head2 = new ListNode(0);
        head2.next = new ListNode(2);
        head2.next.next = new ListNode(4);
  
        // 0->2->4 LinkedList created
        ListNode mergedhead = mergeTwoLists(head1, head2);
  
        printList(mergedhead);
    }
}


Java
// Java Program to Merge Two Sorted
// Linked Lists in New List
// Recursively
  
import java.io.*;
  
public class ListNode {
  
    int val;
    ListNode next;
  
    ListNode() {}
    ListNode(int val) { this.val = val; }
  
    ListNode(int val, ListNode next)
    {
        this.val = val;
        this.next = next;
    }
}
  
class GFG {
  
    public static ListNode mergeTwoLists(ListNode l1,
                                         ListNode l2)
    {
  
        // New List
        ListNode result = null;
  
        // If anyone list is empety then returns the
        // remaining elements of other list
        if (l1 == null) {
            return l2;
        }
        else if (l2 == null) {
            return l1;
        }
  
        // Find the smaller element and recusivly call the
        // function with next node
        if (l1.val <= l2.val) {
            result = l1;
            result.next = mergeTwoLists(l1.next, l2);
        }
        else {
            result = l2;
            result.next = mergeTwoLists(l1, l2.next);
        }
  
        // Return the resultant list
        return (result);
    }
    // A utility function to print linked list
    static void printList(ListNode node)
    {
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        ListNode head1 = new ListNode(23);
        head1.next = new ListNode(35);
        head1.next.next = new ListNode(65);
  
        // 23->35->65 LinkedList created
        ListNode head2 = new ListNode(43);
        head2.next = new ListNode(59);
        head2.next.next = new ListNode(60);
  
        // 43->59->60 LinkedList created
        ListNode mergedhead = mergeTwoLists(head1, head2);
  
        printList(mergedhead);
    }
}


输出

0 1 2 3 4 5 

方法 2:递归方法

可以通过使用递归方法来解决这个问题。

  • 该函数将两个排序列表作为参数。
  • 如果任何列表为空,则它只返回另一个列表中的剩余元素。
  • 否则,它将检查两个列表中较小的元素,将较小的节点附加到结果列表中,并为列表的下一个节点和另一个列表递归调用该函数。

执行:

Java

// Java Program to Merge Two Sorted
// Linked Lists in New List
// Recursively
  
import java.io.*;
  
public class ListNode {
  
    int val;
    ListNode next;
  
    ListNode() {}
    ListNode(int val) { this.val = val; }
  
    ListNode(int val, ListNode next)
    {
        this.val = val;
        this.next = next;
    }
}
  
class GFG {
  
    public static ListNode mergeTwoLists(ListNode l1,
                                         ListNode l2)
    {
  
        // New List
        ListNode result = null;
  
        // If anyone list is empety then returns the
        // remaining elements of other list
        if (l1 == null) {
            return l2;
        }
        else if (l2 == null) {
            return l1;
        }
  
        // Find the smaller element and recusivly call the
        // function with next node
        if (l1.val <= l2.val) {
            result = l1;
            result.next = mergeTwoLists(l1.next, l2);
        }
        else {
            result = l2;
            result.next = mergeTwoLists(l1, l2.next);
        }
  
        // Return the resultant list
        return (result);
    }
    // A utility function to print linked list
    static void printList(ListNode node)
    {
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        ListNode head1 = new ListNode(23);
        head1.next = new ListNode(35);
        head1.next.next = new ListNode(65);
  
        // 23->35->65 LinkedList created
        ListNode head2 = new ListNode(43);
        head2.next = new ListNode(59);
        head2.next.next = new ListNode(60);
  
        // 43->59->60 LinkedList created
        ListNode mergedhead = mergeTwoLists(head1, head2);
  
        printList(mergedhead);
    }
}
输出
23 35 43 59 60 65 

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live