码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】138. Copy List with Random Pointer

时间:2015-08-21 21:16:59      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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.

提示:

此题有两种方法,一种是按照原链表next的顺序依次创建节点,并处理好新链表的next指针,同时把原节点与新节点的对应关系保存到一个hash_map中,然后第二次循环将random指针处理好。这种方法的时间复杂度是O(n),空间复杂度也是O(n)。

第二种方法则是在原链表的每个节点之后插入一个新的节点,这样原节点与新节点的对应关系就已经明确了,因此不需要用hash_map保存,但是需要第三次循环将整个链表拆分成两个。这种方法的时间复杂度是O(n),空间复杂度是O(1)。

但是利用hash_map的方法具有其他的优点,如果在循环中加入一个判断,就可以检测出链表中是否有循环;而第二种方法则不行,会陷入死循环。

代码:

使用hash_map的方法:

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        if (!head) return NULL;
        unordered_map<RandomListNode*, RandomListNode*> mp;
        // 创建一个新的链表头
        RandomListNode *new_head = new RandomListNode(head->label);
        // node1负责指向原链表,node2负责指向新链表
        RandomListNode *node1 = head, *node2 = new_head;
        /**
         * 按照原链表的结构不断创建新的节点,并维护好next指针,将node1与node2的对应关系保存到hash_map中,
         * 以备后面维护random指针的时候,可以通过node1找到对应的node2。
         */
        while (node1->next != NULL) {
            mp[node1] = node2;
            node1 = node1->next;
            node2->next = new RandomListNode(node1->label);
            node2 = node2->next;
        }
        // 将两个链表的尾巴的对应关系也保存好
        mp[node1] = node2;
        
        // 继续从头开始处理random指针
        node1 = head;
        node2 = new_head;
        while (node1->next != NULL) {
            node2->random = mp[node1->random];
            node1 = node1->next;
            node2 = node2->next;
        }
        // 把尾巴的random指针也处理好
        node2->random = mp[node1->random];
        return new_head;
    }
};

不使用hash_map的方法:

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */
class Solution {
public:
    RandomListNode *copyRandomList(RandomListNode *head) {
        /**
         * 假设:l1代表原链表中的节点;l2代表新链表中的节点
         */
        RandomListNode *new_head, *l1, *l2;
        if (head == NULL) return NULL;
        
        /**
         * 第一步:在每一个l1后面创建一个l2,并让l1指向l2,l2指向下一个l1;
         */
        for (l1 = head; l1 != NULL; l1 = l1->next->next) {
            l2 = new RandomListNode(l1->label);
            l2->next = l1->next;
            l1->next = l2;
        }
        
        /**
         * 第二步:给l2的random赋值,l1的random的next指向的就是l2的random的目标;
         */
        new_head = head->next;
        for (l1 = head; l1 != NULL; l1 = l1->next->next) {
            if (l1->random != NULL) l1->next->random = l1->random->next;
        }
        
        /**
         * 第三步:需要将整个链表拆成两个链表,具体做法是让l1的next指向后面的后面;
         *         l2的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 new_head;
    }
};

【LeetCode】138. Copy List with Random Pointer

标签:

原文地址:http://www.cnblogs.com/jdneo/p/4748972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!