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)


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