zoukankan      html  css  js  c++  java
  • 138. Copy List with Random Pointer

    Problem:

    A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

    Return a deep copy of the list.

    The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:

    • val: an integer representing Node.val
    • random_index: the index of the node (range from 0 to n-1) where random pointer points to, or null if it does not point to any node.

    Example 1:

    Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
    Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
    

    Example 2:

    Input: head = [[1,1],[2,1]]
    Output: [[1,1],[2,1]]
    

    Example 3:

    Input: head = [[3,null],[3,0],[3,null]]
    Output: [[3,null],[3,0],[3,null]]
    

    Example 4:

    Input: head = []
    Output: []
    Explanation: Given linked list is empty (null pointer), so return null.
    

    Constraints:

    • -10000 <= Node.val <= 10000
    • Node.random is null or pointing to a node in the linked list.
    • Number of Nodes will not exceed 1000.

    思路1

    Solution:

    Node* copyRandomList(Node* head) {
        Node *newHead, *l1, *l2;
        if (head == NULL)  return NULL;
        for (l1 = head; l1 != NULL; l1 = l1->next->next) {
            l2 = new Node(l1->val);
            l2->next = l1->next;
            l1 -> next = l2;
        }
        
        newHead = head->next;
        for (l1 = head; l1 != NULL; l1 = l1->next->next) {
            if (l1->random != NULL)  l1->next->random = l1->random->next;
        }
        for (l1 = head; l1 != NULL; l1 = l1->next) {
            l2 = l1->next;
            l1->next = l2->next;
            if (l2->next != NULL)  l2->next = l2->next->next;
        }
        return newHead;
    }
    

    性能:
    Runtime: 16 ms  Memory Usage: 11.1 MB

    思路2

    建立一个哈希表unordered_map<Node*, Node*>,key值为指向原链表结点的指针,value为指向复制链表结点的指针,首先遍历原链表,复制出新的链表并用next指针连接。然后遍历原链表,用random指针连接。

    Solution (C++):

    Node* copyRandomList(Node* head) {
        if (!head)  return NULL;
        using N = Node*;
        unordered_map<N, N> hash;
        N old_head = head;
        N new_head = new Node(head->val);
        hash[old_head] = new_head;
        while (old_head->next) {
            new_head->next = new Node(old_head->next->val);
            old_head = old_head->next;
            new_head = new_head->next;
            hash[old_head] = new_head;
        }
        old_head = head;
        new_head = hash[old_head];
        
        while (old_head && new_head) {
            new_head->random = old_head->random ? hash[old_head->random] : NULL;
            old_head = old_head->next;
            new_head = new_head->next;
        }
        
        return hash[head];
    }
    

    性能

    Runtime: 12 ms  Memory Usage: 11.1 MB

    思路3

    思路2的简化版本。

    Solution (C++):

    Node* copyRandomList(Node* head) {
        if (!head)  return NULL;
        Node *newHead, *l1 = head, *l2;
        for (l1; l1; l1 = l1->next->next) {
            l2 = new Node(l1->val);
            l2->next = l1->next;
            l1 -> next = l2;
        }
    
        newHead = head->next;
        for (l1 = head; l1; l1 = l1->next->next) {
            if (l1->random)  l1->next->random = l1->random->next;
        }
        for (l1 = head; l1; l1 = l1->next) {
            l2 = l1->next;
            l1->next = l2->next;
            if (l2->next)  l2->next = l2->next->next;
        }
        return newHead;
    }
    

    性能

    Runtime: 12 ms  Memory Usage: 11.1 MB

    相关链接如下:

    知乎:littledy

    欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可

    作者:littledy
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    可能会搞砸你的面试:你知道一个TCP连接上能发起多少个HTTP请求吗?
    iOS笔记055
    iOS笔记053- Quartz2D-练习
    iOS笔记052- Quartz2D-绘图
    IOS笔记051-手势使用
    IOS笔记050-事件处理
    IOS笔记049-UITabBarController
    IOS笔记048-数据存储
    IOS笔记047-代理传值和block传值
    IOS笔记046-UIApplication/导航控制器
  • 原文地址:https://www.cnblogs.com/dysjtu1995/p/12275040.html
Copyright © 2011-2022 走看看