📌  相关文章
📜  用于合并两个已排序链表的Java程序,使得合并后的链表顺序相反

📅  最后修改于: 2022-05-13 01:54:53.185000             🧑  作者: Mango

用于合并两个已排序链表的Java程序,使得合并后的链表顺序相反

给定两个按升序排序的链表。合并它们,使结果列表按降序(倒序)。

例子:

Input:  a: 5->10->15->40
        b: 2->3->20 
Output: res: 40->20->15->10->5->3->2

Input:  a: NULL
        b: 2->3->20 
Output: res: 20->3->2

一个简单的解决方案是执行以下操作。
1)反转第一个列表'a'。
2)反转第二个列表'b'。
3)合并两个反向列表。
另一个简单的解决方案是首先合并两个列表,然后反转合并的列表。
以上两种方案都需要对链表进行两次遍历。

如何在没有反向、O(1) 辅助空间(就地)且仅遍历两个列表的情况下求解?
这个想法是遵循合并样式过程。将结果列表初始化为空。从头到尾遍历两个列表。比较两个列表的当前节点,并在结果列表的开头插入两个中较小的一个。

1) Initialize result list as empty: res = NULL.
2) Let 'a' and 'b' be heads first and second lists respectively.
3) While (a != NULL and b != NULL)
    a) Find the smaller of two (Current 'a' and 'b')
    b) Insert the smaller value node at the front of result.
    c) Move ahead in the list of smaller node. 
4) If 'b' becomes NULL before 'a', insert all nodes of 'a' 
   into result list at the beginning.
5) If 'a' becomes NULL before 'b', insert all nodes of 'a' 
   into result list at the beginning. 

以下是上述解决方案的实现。

Java
// Java program to merge two sorted
// linked list such that merged
// list is in reverse order
 
// Linked List Class
class LinkedList
{
    // Head of list
    Node head; 
    static Node a, b;
 
    // Node Class
    static class Node
    {
        int data;
        Node next;
 
        // Constructor to create
        // a new node
        Node(int d)
        {
            data = d;
            next = null;
        }
    }
 
    void printlist(Node node)
    {
        while (node != null)
        {
            System.out.print(node.data + " ");
            node = node.next;
        }
    }
 
    Node sortedmerge(Node node1,
                     Node node2)
    {       
        // If both the nodes are null
        if (node1 == null &&
            node2 == null)
        {
            return null;
        }
 
        // Resultant node
        Node res = null;
 
        // If both of them have nodes
        // present traverse them
        while (node1 != null &&
               node2 != null)
        {
 
            // Now compare both nodes
            // current data
            if (node1.data <= node2.data)
            {
                Node temp = node1.next;
                node1.next = res;
                res = node1;
                node1 = temp;
            }
            else
            {
                Node temp = node2.next;
                node2.next = res;
                res = node2;
                node2 = temp;
            }
        }
 
        // If second list reached end,
        // but first list has nodes.
        // Add remaining nodes of first
        // list at the front of result list
        while (node1 != null)
        {
            Node temp = node1.next;
            node1.next = res;
            res = node1;
            node1 = temp;
        }
 
        // If first list reached end,
        // but second list has node.
        // Add remaining nodes of first
        // list at the front of result list
        while (node2 != null)
        {
            Node temp = node2.next;
            node2.next = res;
            res = node2;
            node2 = temp;
        }
 
        return res;
    }
 
    public static void main(String[] args)
    {
        LinkedList list = new LinkedList();
        Node result = null;
 
        /* Let us create two sorted linked lists
           to test the above functions. Created
           lists shall be
           a: 5->10->15
           b: 2->3->20 */
        list.a = new Node(5);
        list.a.next = new Node(10);
        list.a.next.next = new Node(15);
 
        list.b = new Node(2);
        list.b.next = new Node(3);
        list.b.next.next = new Node(20);
 
        System.out.println("List a before merge :");
        list.printlist(a);
        System.out.println("");
        System.out.println("List b before merge :");
        list.printlist(b);
 
        // Merge two sorted linkedlist in
        // decreasing order
        result = list.sortedmerge(a, b);
        System.out.println("");
        System.out.println("Merged linked list : ");
        list.printlist(result);
    }
}
// This code is contributed by Mayank Jaiswal


输出:

List A before merge: 
5 10 15 
List B before merge: 
2 3 20 
Merged Linked List is: 
20 15 10 5 3 2 

时间复杂度: O(N)

辅助空间: O(1)

请参阅有关合并两个排序链表的完整文章,以便合并列表以相反的顺序获取更多详细信息!