{"id":27398,"date":"2018-02-02T21:01:34","date_gmt":"2018-02-02T15:31:34","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27398"},"modified":"2018-02-02T21:01:34","modified_gmt":"2018-02-02T15:31:34","slug":"clone-linked-list-next-random-pointer-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/clone-linked-list-next-random-pointer-2\/","title":{"rendered":"Clone a linked list with next and random pointer | Set 1"},"content":{"rendered":"<p>You are given a Double Link List with one pointer of each node pointing to the next node just like in a single link list. The second pointer however CAN point to any node in the list and not just the previous node. Now write a program in<strong> O(n) time <\/strong>to duplicate this list. That is, write a program which will create a copy of this list.<\/p>\n<p>Let us call the second pointer as arbit pointer as it can point to any arbitrary node in the linked list.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-27411\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ArbitLinked-List12.gif\" alt=\"Clone a linked list with next and random pointer | Set 1 You are given a Double Link Lis\" width=\"520\" height=\"200\" \/><\/p>\n<p>Arbitrary pointers are shown in red and next pointers in black<\/p>\n<p>Figure 1<\/p>\n<p><strong>Method 1 (Uses O(n) extra space)<\/strong><br \/>\nThis method stores the next and arbitrary mappings (of original list) in an array first, then modifies the original Linked List (to create copy), creates a copy. And finally restores the original list.<\/p>\n<p>1) Create all nodes in copy linked list using next pointers.<br \/>\n3) Store the node and its next pointer mappings of original linked list.<br \/>\n3) Change next pointer of all nodes in original linked list to point to the corresponding node in copy linked list.<br \/>\nFollowing diagram shows status of both Linked Lists after above 3 steps. The red arrow shows arbit pointers and black arrow shows next pointers.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-27412\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ArbitLinked-List2.png\" alt=\"Clone a linked list with next and random pointer | Set 1\" width=\"500\" height=\"210\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ArbitLinked-List2.png 500w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/ArbitLinked-List2-300x126.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>4) Change the arbit pointer of all nodes in copy linked list to point to corresponding node in original linked list.<br \/>\n5) Now construct the arbit pointer in copy linked list as below and restore the next pointer of nodes in the original linked list.<\/p>\n<pre>       copy_list_node-&gt;arbit =\r\n                      copy_list_node-&gt;arbit-&gt;arbit-&gt;next;\r\n       copy_list_node = copy_list_node-&gt;next; \r\n<\/pre>\n<p>6) Restore the next pointers in original linked list from the stored mappings(in step 2).<\/p>\n<p>Time Complexity:\u00a0 O(n)<br \/>\nAuxiliary Space:\u00a0 O(n)<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Method 2 (Uses Constant Extra Space)<\/strong><br \/>\nThanks to Saravanan Mani for providing this solution. This solution works using constant space.<br \/>\n1) Create the copy of node 1 and insert it between node 1 &amp; node 2 in original Linked List, create the copy of 2 and insert it between 2 &amp; 3.. Continue in this fashion, add the copy of N afte the Nth node<br \/>\n2) Now copy the arbitrary link in this fashion<\/p>\n<pre>     original-&gt;next-&gt;arbitrary = original-&gt;arbitrary-&gt;next;  \/*TRAVERSE \r\nTWO NODES*\/\r\n<\/pre>\n<p>This works because original-&gt;next is nothing but copy of original and Original-&gt;arbitrary-&gt;next is nothing but copy of arbitrary.<br \/>\n3) Now restore the original and copy linked lists in this fashion in a single loop.<\/p>\n<pre>     original-&gt;next = original-&gt;next-&gt;next;\r\n     copy-&gt;next = copy-&gt;next-&gt;next;\r\n<\/pre>\n<p>4) Make sure that last element of original-&gt;next is NULL.<\/p>\n<p>Refer below post for implementation of this method.<br \/>\n<strong><a href=\"http:\/\/www.geeksforgeeks.org\/clone-linked-list-next-random-pointer-o1-space\/\" target=\"_blank\" rel=\"noopener\">Clone a linked list with next and random pointer in O(1) space<\/a><\/strong><\/p>\n<p>Time Complexity: O(n)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Clone a linked list with next and random pointer &#8211; Doubly linked list &#8211; You are given a Double Link List with one pointer of each node pointing.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79479,79476],"tags":[81525,81529,81524,81528,81522,81523,81526,81527],"class_list":["post-27398","post","type-post","status-publish","format-standard","hentry","category-doubly-linked-list","category-linked-list","tag-clone-a-linked-list-with-next-and-random-pointer-in-c","tag-clone-a-linked-list-with-next-and-random-pointer-leetcode","tag-clone-linked-list-java","tag-copy-linked-list-in-c","tag-copy-list-with-random-pointer-java","tag-copy-list-with-random-pointer-leetcode","tag-copy-list-with-random-pointer-leetcode-c","tag-deep-copy-linked-list-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27398"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27398\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}