leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0138.cpp (526B)


      1 class Solution {
      2   public:
      3     Node *copyRandomList(Node *head) {
      4         if (!head) return nullptr;
      5 
      6         unordered_map<Node *, Node *> um;
      7         Node *h, *t;
      8         t = h = new Node(-1);
      9         for (Node *p = head; p; p = p->next) {
     10             t = t->next = new Node(p->val);
     11             um.insert(make_pair(p, t));
     12         }
     13 
     14         t = h->next;
     15         for (Node *p = head; p; p = p->next, t = t->next) {
     16             if (p->random != nullptr) t->random = um[p->random];
     17         }
     18         return h->next;
     19     }
     20 };