{"id":2180,"date":"2016-09-10T13:08:55","date_gmt":"2016-09-10T05:08:55","guid":{"rendered":"http:\/\/boweihe.me\/?p=2180"},"modified":"2016-09-10T13:08:55","modified_gmt":"2016-09-10T05:08:55","slug":"leetcode-138-copy-list-with-random-pointer","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2180","title":{"rendered":"LeetCode 138. Copy List with Random Pointer"},"content":{"rendered":"<h3>\u601d\u8def<\/h3>\n<p>\u300a\u5251\u6307Offer\u300b#26<br \/>\n\u57fa\u672c\u6b65\u9aa4\u662f\uff1a<\/p>\n<ol>\n<li>\u5728\u6bcf\u4e2a\u8282\u70b9\u540e\u9762\uff0c\u62f7\u8d1d\u4e00\u4e2a\u76f8\u540c\u7684\u8282\u70b9\uff1aA-&gt;A&#8217;-&gt;B-&gt;B&#8217;&#8230;<\/li>\n<li>\u62f7\u8d1d\u968f\u673a\u8282\u70b9\uff0c\u5373A&#8217;-&gt;random\u662fA-&gt;random-&gt;next<\/li>\n<li>\u5206\u79bb\u62f7\u8d1d\u540e\u7684\u94fe\u8868<\/li>\n<\/ol>\n<h3>\u4ee3\u7801<\/h3>\n<p>\uff08\u8111\u62bd\u4e86\u4e00\u4e0b\uff0cbreak\u548ccontinue\u6253\u9519\uff0c\u770b\u4e86\u534a\u5929\uff09<\/p>\n<pre class=\"lang:c++ decode:true \">\/**\n * Definition for singly-linked list with a random pointer.\n * struct RandomListNode {\n *     int label;\n *     RandomListNode *next, *random;\n *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}\n * };\n *\/\nclass Solution {\npublic:\n    void makeNodeDup(RandomListNode* head){\n        \/\/make copy of linked node\n        while(head!=NULL){\n            RandomListNode* replica = new RandomListNode(head-&gt;label);\n            replica-&gt;next = head-&gt;next;\n            head-&gt;next = replica;\n            head = replica-&gt;next;\n        }\n    }\n    void copyRandomLink(RandomListNode* head){\n        \/\/copy random link in new-generated replicas\n        while(head != NULL){\n            if(head-&gt;random == NULL){\n                head = head-&gt;next-&gt;next;\n                continue;\n            }\n            RandomListNode* replica = head-&gt;next;\n            replica-&gt;random = head-&gt;random-&gt;next;\n            head = replica-&gt;next;\n        }\n    }\n    RandomListNode* detachReplica(RandomListNode* head){\n        RandomListNode* replicaHead = NULL;\n        while(head != NULL){\n            RandomListNode* replica = head-&gt;next;\n            if(replicaHead == NULL){\n                replicaHead = replica;\n            }\n            head-&gt;next = replica-&gt;next;\n            if(replica-&gt;next != NULL){\n                replica-&gt;next = replica-&gt;next-&gt;next;\n            } else {\n                replica-&gt;next = NULL;\n            }\n            head = head-&gt;next;\n        }\n        return replicaHead;\n    }\n    RandomListNode *copyRandomList(RandomListNode *head) {\n        makeNodeDup(head);\n        copyRandomLink(head);\n        return detachReplica(head);\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def \u300a\u5251\u6307Offer\u300b#26 \u57fa\u672c\u6b65\u9aa4\u662f\uff1a \u5728\u6bcf\u4e2a\u8282\u70b9\u540e\u9762\uff0c\u62f7\u8d1d\u4e00\u4e2a\u76f8\u540c\u7684\u8282\u70b9\uff1aA-&gt;A&#8217;-&gt;B-&gt;B&#8217;&#8230; \u62f7\u8d1d\u968f\u673a\u8282\u70b9\uff0c\u5373A&#8217;-&gt;random\u662fA-&gt;random-&gt;next \u5206\u79bb\u62f7\u8d1d\u540e\u7684\u94fe\u8868 \u4ee3\u7801 \uff08\u8111\u62bd\u4e86\u4e00\u4e0b\uff0cbreak\u548ccontinue\u6253\u9519\uff0c\u770b\u4e86\u534a\u5929\uff09 \/** * 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: void makeNodeDup(RandomListNode* head){ \/\/make copy of linked node while(head!=NULL){ RandomListNode* replica [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,207],"class_list":["post-2180","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-207"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2180"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2180\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}