Leetcode: Copy List with Random Pointer

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.

顺便练习一下multimap 和Lambda表达式。

/**
 * 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) {
        // Save the random pointer in the map
        unordered_multimap<RandomListNode*, RandomListNode*> random_map;
        typedef unordered_multimap<RandomListNode*, RandomListNode*>::value_type random_pair;
        
        RandomListNode *newhead = NULL;
        RandomListNode *newcur = NULL;
        RandomListNode *cur = head;
        while (cur != NULL) {
            if (newhead == NULL) {
                newhead = newcur = new RandomListNode(cur->label);
            }
            else {
                newcur->next = new RandomListNode(cur->label);
                newcur = newcur->next;
            }
            if (cur->random != NULL) {
                random_map.insert(make_pair(cur->random, newcur));
            }
            cur = cur->next;
        }
        
        // Recreate the random access
        cur = head;
        newcur = newhead;
        while (cur != NULL) {
            if (random_map.count(cur) > 0) {
                auto range = random_map.equal_range(cur);
                for_each(range.first, range.second, [newcur](random_pair &x) {
                        x.second->random = newcur;
                });
            }
            cur = cur->next;
            newcur = newcur->next;
        }
        
        return newhead;
    }
};


============================第二次=======================

不蛮干,用技巧,分三步来做:1)先复制连接成一个链表;2)根据相对关系重建随机节点;3)拆分链表。

/**
 * 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 == NULL) {
            return head;
        }
        
        RandomListNode *cur = head;
        RandomListNode *next = NULL;
        RandomListNode *tmp;
        while (cur != NULL) {
            next = new RandomListNode(cur->label);
            tmp = cur->next;
            cur->next = next;
            cur = tmp;
            next->next = cur;
        }
        
        cur = head;
        while (cur != NULL) {
            if (cur->random != NULL) {
                cur->next->random = cur->random->next;
            }
            cur = cur->next->next;
        }
        
        RandomListNode *copied = new RandomListNode(0);
        tmp = copied;
        cur = head;
        while (cur != NULL) {
            copied->next = cur->next;
            copied = copied->next;
            cur->next = copied->next;
            cur = cur->next;
        }
        
        copied = tmp->next;
        delete tmp;
        
        return copied;
    }
};


你可能感兴趣的:(LeetCode,map,lambda)