{"id":6178,"date":"2020-01-29T20:12:20","date_gmt":"2020-01-30T04:12:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6178"},"modified":"2020-01-29T20:15:52","modified_gmt":"2020-01-30T04:15:52","slug":"leetcode-138-copy-list-with-random-pointer","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-138-copy-list-with-random-pointer\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 138. Copy List with Random Pointer"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>Return a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Object_copying#Deep_copy\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>deep copy<\/strong><\/a>&nbsp;of the list.<\/p>\n\n\n\n<p>The Linked List is represented in the input\/output as a list of&nbsp;<code>n<\/code>&nbsp;nodes. Each node is represented as a pair of&nbsp;<code>[val, random_index]<\/code>&nbsp;where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>val<\/code>: an integer representing&nbsp;<code>Node.val<\/code><\/li><li><code>random_index<\/code>: the index of the node (range from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n-1<\/code>) where random pointer points to, or&nbsp;<code>null<\/code>&nbsp;if it does not point to any node.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/18\/e1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [[7,null],[13,0],[11,4],[10,2],[1,0]]\n<strong>Output:<\/strong> [[7,null],[13,0],[11,4],[10,2],[1,0]]\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/18\/e2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [[1,1],[2,1]]\n<strong>Output:<\/strong> [[1,1],[2,1]]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/12\/18\/e3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [[3,null],[3,0],[3,null]]\n<strong>Output:<\/strong> [[3,null],[3,0],[3,null]]\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = []\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> Given linked list is empty (null pointer), so return null.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>-10000 &lt;= Node.val &lt;= 10000<\/code><\/li><li><code>Node.random<\/code>&nbsp;is null or pointing to a node in the linked list.<\/li><li>Number of Nodes will not exceed 1000.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  Node* copyRandomList(Node* head) {\n    if (!head) return head;\n\n    unordered_map<Node*, Node*> m;    \n    Node* cur = m[head] = new Node(head->val);\n    Node* ans = cur;\n\n    while (head) {\n      if (head->random) {\n        auto it = m.find(head->random);\n        if (it == end(m))\n          it = m.emplace(head->random, new Node(head->random->val)).first;\n        cur->random = it->second;\n      }\n\n      if (head->next) {\n        auto it = m.find(head->next);\n        if (it == end(m))\n          it = m.emplace(head->next, new Node(head->next->val)).first;        \n        cur->next = it->second;\n      }\n\n      head = head->next;\n      cur = cur->next;\n    }\n    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[77,82,462,177,376],"class_list":["post-6178","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-graph","tag-hashtable","tag-map","tag-medium","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6178"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6178\/revisions"}],"predecessor-version":[{"id":6180,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6178\/revisions\/6180"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}