📜  扁平化链表的Java程序

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

扁平化链表的Java程序

给定一个链表,其中每个节点都表示一个链表并包含其类型的两个指针:

  1. 指向主列表中下一个节点的指针(我们在下面的代码中称其为“右”指针)。
  2. 指向该节点指向的链表的指针(我们在下面的代码中将其称为“向下”指针)。

所有链表都已排序。请参阅以下示例

5 -> 10 -> 19 -> 28
       |    |     |     |
       V    V     V     V
       7    20    22    35
       |          |     |
       V          V     V
       8          50    40
       |                |
       V                V
       30               45

编写一个函数flatten() 将列表展平为单个链表。扁平化的链表也应该排序。比如上面的输入列表,输出列表应该是5->7->8->10->19->20->22->28->30->35->40->45->50 .

这个想法是对链表使用合并排序的 Merge() 过程。我们使用 merge() 来一个一个地合并列表。我们递归地合并()当前列表与已经展平的列表。
向下指针用于链接展平列表的节点。

下面是上述方法的实现:

Java
// Java program for flattening 
// a Linked List
class LinkedList
{
    // Head of list
    Node head;  
  
    // Linked list Node
    class Node
    {
        int data;
        Node right, down;
        Node(int data)
        {
            this.data = data;
            right = null;
            down = null;
        }
    }
  
    // An utility function to merge 
    // two sorted linked lists
    Node merge(Node a, Node b)
    {
        // If first linked list is empty 
        // then second is the answer
        if (a == null)     
            return b;
  
        // If second linked list is empty 
        // then first is the result
        if (b == null)      
            return a;
  
        // Compare the data members of the 
        // two linked lists and put the 
        // larger one in the result
        Node result;
  
        if (a.data < b.data)
        {
            result = a;
            result.down = merge(a.down, b);
        }
  
        else
        {
            result = b;
            result.down = merge(a, b.down);
        }
  
        result.right = null; 
        return result;
    }
  
    Node flatten(Node root)
    {
        // Base Cases
        if (root == null || 
            root.right == null)
            return root;
  
        // Recur for list on right
        root.right = flatten(root.right);
  
        // Now merge
        root = merge(root, root.right);
  
        // Return the root
        // it will be in turn merged with 
        // its left
        return root;
    }
  
    /* Utility function to insert a node 
       at beginning of the linked list */
    Node push(Node head_ref, int data)
    {
        /* 1 & 2: Allocate the Node &
                  Put in the data */
        Node new_node = new Node(data);
  
        // 3. Make next of new Node as head 
        new_node.down = head_ref;
  
        // 4. Move the head to point to 
        // new Node 
        head_ref = new_node;
  
        // 5. return to link it back 
        return head_ref;
    }
  
    void printList()
    {
        Node temp = head;
        while (temp != null)
        {
            System.out.print(temp.data + " ");
            temp = temp.down;
        }
        System.out.println();
    }
  
    // Driver code
    public static void main(String args[])
    {
        LinkedList L = new LinkedList();
  
        /* Create the following linked list
            5 -> 10 -> 19 -> 28
            |    |     |     |
            V    V     V     V
            7    20    22    35
            |          |     |
            V          V     V
            8          50    40
            |                |
            V                V
            30               45 */
        L.head = L.push(L.head, 30);
        L.head = L.push(L.head, 8);
        L.head = L.push(L.head, 7);
        L.head = L.push(L.head, 5);
  
        L.head.right = 
        L.push(L.head.right, 20);
        L.head.right = 
        L.push(L.head.right, 10);
  
        L.head.right.right = 
        L.push(L.head.right.right, 50);
        L.head.right.right = 
        L.push(L.head.right.right, 22);
        L.head.right.right = 
        L.push(L.head.right.right, 19);
  
        L.head.right.right.right = 
        L.push(L.head.right.right.right, 45);
        L.head.right.right.right = 
        L.push(L.head.right.right.right, 40);
        L.head.right.right.right = 
        L.push(L.head.right.right.right, 35);
        L.head.right.right.right = 
        L.push(L.head.right.right.right, 20);
  
        // Flatten the list
        L.head = L.flatten(L.head);
  
        L.printList();
    }
} 
// This code is contributed by Rajat Mishra


输出:

5 7 8 10 19 20 20 22 30 35 40 45 50

有关详细信息,请参阅有关扁平化链接列表的完整文章!