📌  相关文章
📜  用于克隆具有 Next 和随机指针的链表的 C++ 程序集 2

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

用于克隆具有 Next 和随机指针的链表的 C++ 程序集 2

我们已经讨论了克隆链表的两种不同方法。在这篇文章中,讨论了一种更简单的克隆链表的方法。

这个想法是使用散列。下面是算法。

  1. 遍历原始链表,在数据方面进行复制。
  2. 用原始链表节点和复制的链表节点制作键值对的哈希映射。
  3. 再次遍历原始链表并使用哈希图调整克隆链表节点的下一个和随机引用。

下面是上述方法的实现。

C++
// C++ program to clone a linked list 
// with random pointers
#include
using namespace std;
  
// Linked List Node
class Node
{
    public:
  
    // Node data
    int data;
      
    // Next and random reference
    Node *next, *random;
  
    Node(int data)
    {
        this->data = data;
        this->next = 
        this->random = NULL;
    }
};
  
// Linked list class
class LinkedList
{
    public:
  
    // Linked list head reference
    Node *head;
  
    LinkedList(Node *head)
    {
        this->head = head;
    }
  
    // Push method to put data always at
    // the head in the linked list.
    void push(int data)
    {
        Node *node = new Node(data);
        node->next = head;
        head = node;
    }
  
    // Method to print the list.
    void print()
    {
        Node *temp = head;
        while (temp != NULL)
        {
            Node *random = temp->random;
            int randomData = ((random != NULL) ?
                               random->data : -1);
            cout << "Data = " << 
                    temp->data << ", ";
            cout << "Random Data = " << 
                     randomData << endl;
            temp = temp->next;
        }
        cout << endl;
    }
  
    // Actual clone method which returns
    // head reference of cloned linked
    // list.
    LinkedList* clone()
    {
        // Initialize two references,
        // one with original list's head.
        Node *origCurr = head;
        Node *cloneCurr = NULL;
  
        // Hash map which contains node 
        // to node mapping of original 
        // and clone linked list.
        unordered_map mymap;
  
        // Traverse the original list and
        // make a copy of that in the 
        // clone linked list.
        while (origCurr != NULL)
        {
            cloneCurr = new Node(origCurr->data);
            mymap[origCurr] = cloneCurr;
            origCurr = origCurr->next;
        }
  
        // Adjusting the original list 
        // reference again.
        origCurr = head;
  
        // Traversal of original list again
        // to adjust the next and random 
        // references of clone list using 
        // hash map.
        while (origCurr != NULL)
        {
            cloneCurr = mymap[origCurr];
            cloneCurr->next = 
            mymap[origCurr->next];
            cloneCurr->random = 
            mymap[origCurr->random];
            origCurr = origCurr->next;
        }
  
        // return the head reference of 
        // the clone list.
        return new LinkedList(mymap[head]);
    }
};
  
// Driver code
int main()
{
    // Pushing data in the linked list.
    LinkedList *mylist = 
                new LinkedList(new Node(5));
    mylist->push(4);
    mylist->push(3);
    mylist->push(2);
    mylist->push(1);
  
    // Setting up random references.
    mylist->head->random = 
    mylist->head->next->next;
  
    mylist->head->next->random =
    mylist->head->next->next->next;
  
    mylist->head->next->next->random =
    mylist->head->next->next->next->next;
  
    mylist->head->next->next->next->random =
    mylist->head->next->next->next->next->next;
  
    mylist->head->next->next->next->next->random =
    mylist->head->next;
  
    // Making a clone of the original
    // linked list.
    LinkedList *clone = mylist->clone();
  
    // Print the original and cloned 
    // linked list.
    cout << "Original linked list";
    mylist->print();
    cout << "Cloned linked list";
    clone->print();
}
// This code is contributed by Chhavi


输出:

Original linked list
Data = 1, Random data = 3
Data = 2, Random data = 4
Data = 3, Random data = 5
Data = 4, Random data = -1
Data = 5, Random data = 2

Cloned linked list
Data = 1, Random data = 3
Data = 2, Random data = 4
Data = 3, Random data = 5
Data = 4, Random data = -1
Data = 5, Random data = 2

时间复杂度: O(n)
辅助空间:O(n)
请参考完整的文章 Clone a linked list with next and random pointer |设置2了解更多详情!